|
|
|
@ -73,25 +73,6 @@ func (f *Function) Compile() {
|
|
|
|
|
f.Assembler.Return()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PrintAsm shows the assembly instructions.
|
|
|
|
|
func (f *Function) PrintAsm() {
|
|
|
|
|
ansi.Bold.Println(f.Name)
|
|
|
|
|
ansi.Dim.Println("╭────────────────────────────────────────────────────────────")
|
|
|
|
|
|
|
|
|
|
for _, x := range f.Assembler.Instructions {
|
|
|
|
|
ansi.Dim.Print("│ ")
|
|
|
|
|
fmt.Print(x.Mnemonic.String())
|
|
|
|
|
|
|
|
|
|
if x.Data != nil {
|
|
|
|
|
fmt.Print(" " + x.Data.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Print("\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ansi.Dim.Println("╰────────────────────────────────────────────────────────────")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CompileInstruction compiles a single instruction.
|
|
|
|
|
func (f *Function) CompileInstruction(line token.List) error {
|
|
|
|
|
if len(line) == 0 {
|
|
|
|
@ -99,6 +80,30 @@ func (f *Function) CompileInstruction(line token.List) error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if line[0].Kind == token.Keyword {
|
|
|
|
|
return f.CompileKeyword(line)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expr := expression.Parse(line)
|
|
|
|
|
|
|
|
|
|
if expr == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer expr.Close()
|
|
|
|
|
|
|
|
|
|
if isVariableDefinition(expr) {
|
|
|
|
|
return f.CompileVariableDefinition(expr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isFunctionCall(expr) {
|
|
|
|
|
return f.CompileFunctionCall(expr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errors.New(&errors.InvalidInstruction{Instruction: expr.Token.Text()}, f.File, expr.Token.Position)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CompileKeyword compiles an instruction that starts with a keyword.
|
|
|
|
|
func (f *Function) CompileKeyword(line token.List) error {
|
|
|
|
|
switch line[0].Text() {
|
|
|
|
|
case "return":
|
|
|
|
|
if len(line) > 1 {
|
|
|
|
@ -112,21 +117,12 @@ func (f *Function) CompileInstruction(line token.List) error {
|
|
|
|
|
default:
|
|
|
|
|
return errors.New(&errors.KeywordNotImplemented{Keyword: line[0].Text()}, f.File, line[0].Position)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expr := expression.Parse(line)
|
|
|
|
|
|
|
|
|
|
if expr == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer expr.Close()
|
|
|
|
|
|
|
|
|
|
if expr.Token.Kind == token.Number || expr.Token.Kind == token.Identifier || expr.Token.Kind == token.String {
|
|
|
|
|
return errors.New(&errors.InvalidInstruction{Instruction: expr.Token.Text()}, f.File, expr.Token.Position)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if expr.Token.Text() == ":=" {
|
|
|
|
|
// CompileVariableDefinition compiles a variable definition.
|
|
|
|
|
func (f *Function) CompileVariableDefinition(expr *expression.Expression) error {
|
|
|
|
|
if len(expr.Children) < 2 {
|
|
|
|
|
return errors.New(errors.MissingAssignValue, f.File, expr.LastChild().Token.After())
|
|
|
|
|
}
|
|
|
|
@ -141,7 +137,7 @@ func (f *Function) CompileInstruction(line token.List) error {
|
|
|
|
|
value := expr.Children[1]
|
|
|
|
|
|
|
|
|
|
// All expressions are returned to the memory pool.
|
|
|
|
|
// To avoid losing variable values, we will remove it from the expression.
|
|
|
|
|
// To avoid losing variable values, we will detach it from the expression.
|
|
|
|
|
expr.RemoveChild(value)
|
|
|
|
|
|
|
|
|
|
f.Variables[name] = &Variable{
|
|
|
|
@ -153,7 +149,8 @@ func (f *Function) CompileInstruction(line token.List) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if expr.Token.Text() == "λ" {
|
|
|
|
|
// CompileFunctionCall compiles a function call.
|
|
|
|
|
func (f *Function) CompileFunctionCall(expr *expression.Expression) error {
|
|
|
|
|
funcName := expr.Children[0].Token.Text()
|
|
|
|
|
parameters := expr.Children[1:]
|
|
|
|
|
|
|
|
|
@ -193,10 +190,36 @@ func (f *Function) CompileInstruction(line token.List) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
// PrintAsm shows the assembly instructions.
|
|
|
|
|
func (f *Function) PrintAsm() {
|
|
|
|
|
ansi.Bold.Println(f.Name)
|
|
|
|
|
ansi.Dim.Println("╭────────────────────────────────────────────────────────────")
|
|
|
|
|
|
|
|
|
|
for _, x := range f.Assembler.Instructions {
|
|
|
|
|
ansi.Dim.Print("│ ")
|
|
|
|
|
fmt.Print(x.Mnemonic.String())
|
|
|
|
|
|
|
|
|
|
if x.Data != nil {
|
|
|
|
|
fmt.Print(" " + x.Data.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Print("\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ansi.Dim.Println("╰────────────────────────────────────────────────────────────")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// String returns the function name.
|
|
|
|
|
func (f *Function) String() string {
|
|
|
|
|
return f.Name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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() == ":="
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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() == "λ"
|
|
|
|
|
}
|
|
|
|
|