Added a rudimentary FizzBuzz example

This commit is contained in:
Eduard Urbach 2024-08-03 01:11:22 +02:00
parent df5813515d
commit 6aa1e674df
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
7 changed files with 48 additions and 6 deletions

View File

@ -0,0 +1,39 @@
import log
import sys
main() {
fizzbuzz(15)
}
fizzbuzz(n) {
x := 1
loop {
// TODO: implement switch statement
if x % 15 == 0 {
print("FizzBuzz", 8)
} else {
if x % 5 == 0 {
print("Buzz", 4)
} else {
if x % 3 == 0 {
print("Fizz", 4)
} else {
log.number(x)
}
}
}
x += 1
if x > n {
return
} else {
print(" ", 1)
}
}
}
print(address, length) {
sys.write(1, address, length)
}

View File

@ -3,6 +3,7 @@ package asm
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"strings"
"git.akyoto.dev/cli/q/src/build/arch/x64" "git.akyoto.dev/cli/q/src/build/arch/x64"
"git.akyoto.dev/cli/q/src/build/config" "git.akyoto.dev/cli/q/src/build/config"
@ -287,6 +288,10 @@ restart:
} }
for key, address := range labels { for key, address := range labels {
if strings.HasPrefix(key, "data_") {
continue
}
if address > pointer.Position { if address > pointer.Position {
labels[key] += offset labels[key] += offset
} }

View File

@ -7,7 +7,7 @@ import (
// AddBytes adds a sequence of bytes and returns its address as a label. // AddBytes adds a sequence of bytes and returns its address as a label.
func (f *Function) AddBytes(value []byte) string { func (f *Function) AddBytes(value []byte) string {
f.count.data++ f.count.data++
label := fmt.Sprintf("%s_data_%d", f.UniqueName, f.count.data) label := fmt.Sprintf("data_%s_%d", f.UniqueName, f.count.data)
f.Assembler.SetData(label, value) f.Assembler.SetData(label, value)
return label return label
} }

View File

@ -14,7 +14,6 @@ type Scope struct {
// AddVariable adds a new variable to the current scope. // AddVariable adds a new variable to the current scope.
func (s *Scope) AddVariable(variable *Variable) { func (s *Scope) AddVariable(variable *Variable) {
variable.Depth = s.Depth
s.Variables = append(s.Variables, variable) s.Variables = append(s.Variables, variable)
s.Use(variable.Register) s.Use(variable.Register)
} }

View File

@ -47,7 +47,6 @@ func (stack *Stack) PushScope(body ast.AST, buffer []byte) *Scope {
Name: v.Name, Name: v.Name,
Register: v.Register, Register: v.Register,
Alive: count, Alive: count,
Depth: uint8(len(stack.Scopes)),
}) })
} }
} }
@ -58,8 +57,8 @@ func (stack *Stack) PushScope(body ast.AST, buffer []byte) *Scope {
// UseVariable reduces the lifetime of the variable in all scopes. // UseVariable reduces the lifetime of the variable in all scopes.
func (stack *Stack) UseVariable(variable *Variable) { func (stack *Stack) UseVariable(variable *Variable) {
for depth, scope := range stack.Scopes { for _, scope := range stack.Scopes {
if scope.InLoop && variable.Depth != uint8(depth) { if scope.InLoop {
continue continue
} }

View File

@ -8,7 +8,6 @@ import (
type Variable struct { type Variable struct {
Name string Name string
Alive uint8 Alive uint8
Depth uint8
Register cpu.Register Register cpu.Register
} }

View File

@ -23,6 +23,7 @@ var examples = []struct {
{"echo", "Echo", "Echo", 0}, {"echo", "Echo", "Echo", 0},
{"itoa", "", "9223372036854775807", 0}, {"itoa", "", "9223372036854775807", 0},
{"collatz", "", "6 3 10 5 16 8 4 2 1", 0}, {"collatz", "", "6 3 10 5 16 8 4 2 1", 0},
{"fizzbuzz", "", "1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz", 0},
} }
func TestExamples(t *testing.T) { func TestExamples(t *testing.T) {