Skip to content

Commit

Permalink
feat: cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
0daryo committed Dec 7, 2019
1 parent e191e59 commit a4226fa
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cmd/test/interpreter.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
20 changes: 20 additions & 0 deletions cmd/test/repl.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit a4226fa

Please sign in to comment.