diff --git a/cmd/test/interpreter.go b/cmd/test/interpreter.go new file mode 100644 index 0000000..01b1134 --- /dev/null +++ b/cmd/test/interpreter.go @@ -0,0 +1,48 @@ +package cmd + +import ( + "flag" + "fmt" + "io" + "io/ioutil" + "os" + + "github.com/0daryo/ody/evaluator" + "github.com/0daryo/ody/lexer" + "github.com/0daryo/ody/object" + "github.com/0daryo/ody/parser" +) + +func Interprete() { + flag.Parse() + // ファイルをOpenする + f, err := os.Open(flag.Arg(0)) + if err != nil { + fmt.Println("error") + } + defer f.Close() + + // 一気に全部読み取り + b, err := ioutil.ReadAll(f) + // 出力 + l := lexer.New(string(b)) + p := parser.New(l) + + program := p.ParseProgram() + out := os.Stdout + if len(p.Errors()) != 0 { + printParserErrors(out, p.Errors()) + } + env := object.NewEnvironment() + evaluated := evaluator.Eval(program, env) + if evaluated != nil { + io.WriteString(out, evaluated.Inspect()) + io.WriteString(out, "\n") + } +} + +func printParserErrors(out io.Writer, errors []string) { + for _, msg := range errors { + io.WriteString(out, "\t"+msg+"\n") + } +} diff --git a/cmd/test/repl.go b/cmd/test/repl.go new file mode 100644 index 0000000..1706a1f --- /dev/null +++ b/cmd/test/repl.go @@ -0,0 +1,20 @@ +package cmd + +import ( + "fmt" + "os" + "os/user" + + "github.com/0daryo/ody/repl" +) + +func Exec() { + user, err := user.Current() + if err != nil { + panic(err) + } + fmt.Printf("Hello %s! This is the ody programming language!\n", + user.Username) + fmt.Printf("Feel free to type in commands\n") + repl.Start(os.Stdin, os.Stdout) +}