forked from goby-lang/goby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.go
35 lines (32 loc) · 1002 Bytes
/
compiler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package compiler
import (
"fmt"
"github.com/goby-lang/goby/compiler/bytecode"
"github.com/goby-lang/goby/compiler/lexer"
"github.com/goby-lang/goby/compiler/parser"
)
// CompileToBytecode compiles input source code into Goby bytecode
func CompileToBytecode(input string) (string, error) {
l := lexer.New(input)
p := parser.New(l)
program, err := p.ParseProgram()
if err != nil {
return "", fmt.Errorf(err.Message)
}
g := bytecode.NewGenerator()
g.InitTopLevelScope(program)
return g.GenerateByteCode(program.Statements), nil
}
// CompileToInstructions compiles input source code into instruction set data structures
func CompileToInstructions(input string, parserMode int) ([]*bytecode.InstructionSet, error) {
l := lexer.New(input)
p := parser.New(l)
p.Mode = parserMode
program, err := p.ParseProgram()
if err != nil {
return nil, fmt.Errorf(err.Message)
}
g := bytecode.NewGenerator()
g.InitTopLevelScope(program)
return g.GenerateInstructions(program.Statements), nil
}