Implemented assignments

This commit is contained in:
2024-06-25 17:09:25 +02:00
parent c7d90a2d11
commit 2569c1bf63
5 changed files with 58 additions and 35 deletions

View File

@ -109,37 +109,32 @@ func (f *Function) CompileInstruction(line token.List) error {
return f.CompileVariableDefinition(expr)
}
if isAssignment(expr) {
return f.CompileAssignment(expr)
}
if isFunctionCall(expr) {
return f.CompileFunctionCall(expr)
}
if expr.Token.Kind == token.Operator {
name := expr.Children[0].Token.Text()
number, _ := strconv.Atoi(expr.Children[1].Token.Text())
register := f.Variables[name].Register
switch expr.Token.Text() {
case "+=":
f.Assembler.RegisterNumber(asm.ADD, register, number)
return nil
case "-=":
f.Assembler.RegisterNumber(asm.SUB, register, number)
return nil
case "*=":
f.Assembler.RegisterNumber(asm.MUL, register, number)
return nil
case "/=":
f.Assembler.RegisterNumber(asm.DIV, register, number)
return nil
}
}
return errors.New(&errors.InvalidInstruction{Instruction: expr.Token.Text()}, f.File, expr.Token.Position)
}
// isAssignment returns true if the expression is an assignment.
func isAssignment(expr *expression.Expression) bool {
return expr.Token.Kind == token.Operator && expr.Token.Bytes[len(expr.Token.Bytes)-1] == '='
}
// isFunctionCall returns true if the expression is a function call.
func isFunctionCall(expr *expression.Expression) bool {
return expr.Token.Kind == token.Operator && expr.Token.Text() == "λ"
}
// isVariableDefinition returns true if the expression is a variable definition.
func isVariableDefinition(expr *expression.Expression) bool {
return expr.Token.Kind == token.Operator && expr.Token.Text() == ":="
}
// 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() {