Added more tests
This commit is contained in:
@ -156,29 +156,10 @@ func (f *Function) CompileFunctionCall(expr *expression.Expression) error {
|
||||
parameters := expr.Children[1:]
|
||||
|
||||
for i, parameter := range parameters {
|
||||
switch parameter.Token.Kind {
|
||||
case token.Identifier:
|
||||
name := parameter.Token.Text()
|
||||
variable, exists := f.Variables[name]
|
||||
err := f.ExpressionToRegister(parameter, f.CPU.Syscall[i])
|
||||
|
||||
if !exists {
|
||||
panic("Unknown identifier " + name)
|
||||
}
|
||||
|
||||
if !variable.IsConst {
|
||||
panic("Not implemented yet")
|
||||
}
|
||||
|
||||
n, _ := strconv.Atoi(variable.Value.Token.Text())
|
||||
f.Assembler.MoveRegisterNumber(f.CPU.Syscall[i], uint64(n))
|
||||
|
||||
case token.Number:
|
||||
value := parameter.Token.Text()
|
||||
n, _ := strconv.Atoi(value)
|
||||
f.Assembler.MoveRegisterNumber(f.CPU.Syscall[i], uint64(n))
|
||||
|
||||
default:
|
||||
panic("Unknown expression")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@ -191,6 +172,59 @@ func (f *Function) CompileFunctionCall(expr *expression.Expression) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExpressionToRegister moves the result of an expression into the given register.
|
||||
func (f *Function) ExpressionToRegister(root *expression.Expression, register cpu.Register) error {
|
||||
if root.IsLeaf() {
|
||||
return f.TokenToRegister(root.Token, register)
|
||||
}
|
||||
|
||||
return errors.New(errors.NotImplemented, f.File, root.Token.Position)
|
||||
}
|
||||
|
||||
// TokenToRegister moves a token into a register.
|
||||
// It only works with identifiers, numbers and strings.
|
||||
func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
|
||||
switch t.Kind {
|
||||
case token.Identifier:
|
||||
name := t.Text()
|
||||
variable, exists := f.Variables[name]
|
||||
|
||||
if !exists {
|
||||
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, t.Position)
|
||||
}
|
||||
|
||||
if !variable.IsConst {
|
||||
return errors.New(errors.NotImplemented, f.File, t.Position)
|
||||
}
|
||||
|
||||
n, err := strconv.Atoi(variable.Value.Token.Text())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.Assembler.MoveRegisterNumber(register, uint64(n))
|
||||
return nil
|
||||
|
||||
case token.Number:
|
||||
value := t.Text()
|
||||
n, err := strconv.Atoi(value)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.Assembler.MoveRegisterNumber(register, uint64(n))
|
||||
return nil
|
||||
|
||||
case token.String:
|
||||
return errors.New(errors.NotImplemented, f.File, t.Position)
|
||||
|
||||
default:
|
||||
return errors.New(errors.InvalidExpression, f.File, t.Position)
|
||||
}
|
||||
}
|
||||
|
||||
// PrintAsm shows the assembly instructions.
|
||||
func (f *Function) PrintAsm() {
|
||||
ansi.Bold.Println(f.Name)
|
||||
|
Reference in New Issue
Block a user