Implemented else blocks
This commit is contained in:
parent
ff86dfe590
commit
e537e543cc
17 changed files with 118 additions and 25 deletions
|
@ -3,15 +3,20 @@ package core
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/build/asm"
|
||||
"git.akyoto.dev/cli/q/src/build/ast"
|
||||
)
|
||||
|
||||
// CompileIf compiles a branch instruction.
|
||||
func (f *Function) CompileIf(branch *ast.If) error {
|
||||
f.count.branch++
|
||||
success := fmt.Sprintf("%s_if_%d_true", f.Name, f.count.branch)
|
||||
fail := fmt.Sprintf("%s_if_%d_false", f.Name, f.count.branch)
|
||||
err := f.CompileCondition(branch.Condition, success, fail)
|
||||
|
||||
var (
|
||||
end string
|
||||
success = fmt.Sprintf("%s_if_%d_true", f.Name, f.count.branch)
|
||||
fail = fmt.Sprintf("%s_if_%d_false", f.Name, f.count.branch)
|
||||
err = f.CompileCondition(branch.Condition, success, fail)
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -20,7 +25,31 @@ func (f *Function) CompileIf(branch *ast.If) error {
|
|||
f.AddLabel(success)
|
||||
f.PushScope(branch.Body, f.File.Bytes)
|
||||
err = f.CompileAST(branch.Body)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if branch.Else != nil {
|
||||
end = fmt.Sprintf("%s_if_%d_end", f.Name, f.count.branch)
|
||||
f.Jump(asm.JUMP, end)
|
||||
}
|
||||
|
||||
f.PopScope()
|
||||
f.AddLabel(fail)
|
||||
return err
|
||||
|
||||
if branch.Else != nil {
|
||||
f.PushScope(branch.Else, f.File.Bytes)
|
||||
err = f.CompileAST(branch.Else)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.PopScope()
|
||||
f.AddLabel(end)
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue