bunch of changes
This commit is contained in:
27
monkey.go
27
monkey.go
@@ -4,6 +4,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"monkey/internal/object"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
@@ -42,7 +44,7 @@ func decode(path string) (*compiler.Bytecode, error) {
|
||||
return compiler.Decode(b), nil
|
||||
}
|
||||
|
||||
func compile(path string) (bc *compiler.Bytecode, err error) {
|
||||
func compile(path string, debug bool) (bc *compiler.Bytecode, err error) {
|
||||
input := string(mustReadFile(path))
|
||||
res, errs := parser.Parse(path, input)
|
||||
if len(errs) > 0 {
|
||||
@@ -55,7 +57,12 @@ func compile(path string) (bc *compiler.Bytecode, err error) {
|
||||
return nil, errors.New(buf.String())
|
||||
}
|
||||
|
||||
if debug {
|
||||
log.Printf("AST:\n%s\n", res)
|
||||
}
|
||||
|
||||
c := compiler.New()
|
||||
c.Debug = debug
|
||||
c.SetFileInfo(path, input)
|
||||
if err = c.Compile(res); err != nil {
|
||||
return
|
||||
@@ -64,13 +71,15 @@ func compile(path string) (bc *compiler.Bytecode, err error) {
|
||||
return c.Bytecode(), nil
|
||||
}
|
||||
|
||||
func ExecFileVM(f string) (err error) {
|
||||
func ExecFileVM(f string, args []string, debug bool) (err error) {
|
||||
var bytecode *compiler.Bytecode
|
||||
|
||||
if filepath.Ext(f) == ".monkeyc" {
|
||||
object.Args = args
|
||||
|
||||
if filepath.Ext(f) == ".mc" {
|
||||
bytecode, err = decode(f)
|
||||
} else {
|
||||
bytecode, err = compile(f)
|
||||
bytecode, err = compile(f, debug)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@@ -79,6 +88,7 @@ func ExecFileVM(f string) (err error) {
|
||||
}
|
||||
|
||||
mvm := vm.New(f, bytecode)
|
||||
mvm.Debug = debug
|
||||
if err = mvm.Run(); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
@@ -87,7 +97,7 @@ func ExecFileVM(f string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func CompileFiles(files []string) error {
|
||||
func CompileFiles(files []string, debug bool) error {
|
||||
for _, f := range files {
|
||||
b := mustReadFile(f)
|
||||
|
||||
@@ -99,7 +109,12 @@ func CompileFiles(files []string) error {
|
||||
return ErrParseError
|
||||
}
|
||||
|
||||
if debug {
|
||||
log.Printf("AST:\n%s\n", res)
|
||||
}
|
||||
|
||||
c := compiler.New()
|
||||
c.Debug = debug
|
||||
if err := c.Compile(res); err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
@@ -112,7 +127,7 @@ func CompileFiles(files []string) error {
|
||||
}
|
||||
|
||||
ext := filepath.Ext(f)
|
||||
writeFile(f[:len(f)-len(ext)]+".monkeyc", cnt)
|
||||
writeFile(f[:len(f)-len(ext)]+".mc", cnt)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user