Fixed missing register move in if statements
This commit is contained in:
parent
646dafd216
commit
cb6b3a4cd0
@ -4,18 +4,14 @@ import sys
|
||||
sleep(nanoseconds Int) {
|
||||
seconds := 0
|
||||
|
||||
loop {
|
||||
if nanoseconds >= 1000000000 {
|
||||
nanoseconds = nanoseconds - 1000000000
|
||||
seconds += 1
|
||||
} else {
|
||||
seconds, nanoseconds = nanoseconds / 1000000000
|
||||
}
|
||||
|
||||
timespec := mem.alloc(16)
|
||||
store(timespec, 8, seconds)
|
||||
offset := timespec + 8
|
||||
store(offset, 8, nanoseconds)
|
||||
sys.nanosleep(timespec)
|
||||
mem.free(timespec, 16)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"git.akyoto.dev/cli/q/src/asm"
|
||||
"git.akyoto.dev/cli/q/src/ast"
|
||||
"git.akyoto.dev/cli/q/src/errors"
|
||||
"git.akyoto.dev/cli/q/src/expression"
|
||||
"git.akyoto.dev/cli/q/src/token"
|
||||
)
|
||||
|
||||
@ -32,5 +34,28 @@ func (f *Function) CompileAssign(node *ast.Assign) error {
|
||||
return f.CompileAssignDivision(node)
|
||||
}
|
||||
|
||||
return errors.New(errors.NotImplemented, f.File, left.Token.Position)
|
||||
if !ast.IsFunctionCall(right) {
|
||||
return errors.New(errors.NotImplemented, f.File, node.Expression.Token.Position)
|
||||
}
|
||||
|
||||
count := 0
|
||||
_, err := f.CompileCall(right)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return left.EachLeaf(func(leaf *expression.Expression) error {
|
||||
name := leaf.Token.Text(f.File.Bytes)
|
||||
variable := f.VariableByName(name)
|
||||
|
||||
if variable == nil {
|
||||
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, left.Token.Position)
|
||||
}
|
||||
|
||||
f.RegisterRegister(asm.MOVE, variable.Register, f.CPU.Output[count])
|
||||
f.UseVariable(variable)
|
||||
count++
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
@ -9,6 +9,10 @@ import (
|
||||
|
||||
// CompileIf compiles a branch instruction.
|
||||
func (f *Function) CompileIf(branch *ast.If) error {
|
||||
for _, register := range f.CPU.Input {
|
||||
f.SaveRegister(register)
|
||||
}
|
||||
|
||||
f.count.branch++
|
||||
|
||||
var (
|
||||
|
23
tests/programs/branch-save.q
Normal file
23
tests/programs/branch-save.q
Normal file
@ -0,0 +1,23 @@
|
||||
main() {
|
||||
a, b := f(5)
|
||||
assert a == 0
|
||||
assert b == 5
|
||||
|
||||
a, b = f(15)
|
||||
assert a == 1
|
||||
assert b == 5
|
||||
|
||||
a, b = f(25)
|
||||
assert a == 2
|
||||
assert b == 5
|
||||
}
|
||||
|
||||
f(b Int) -> (Int, Int) {
|
||||
a := 0
|
||||
|
||||
if b >= 10 {
|
||||
a, b = b / 10
|
||||
}
|
||||
|
||||
return a, b
|
||||
}
|
@ -54,6 +54,7 @@ var programs = []struct {
|
||||
{"branch-and", "", "", 0},
|
||||
{"branch-or", "", "", 0},
|
||||
{"branch-both", "", "", 0},
|
||||
{"branch-save", "", "", 0},
|
||||
{"jump-near", "", "", 0},
|
||||
{"switch", "", "", 0},
|
||||
{"loop", "", "", 0},
|
||||
|
Loading…
x
Reference in New Issue
Block a user