Skip to content

Commit

Permalink
add basic ability to parse files
Browse files Browse the repository at this point in the history
Signed-off-by: Flipez <code@brauser.io>
  • Loading branch information
Flipez committed Sep 28, 2021
1 parent f6c8642 commit 1bcf34c
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
package main

import (
"fmt"
"io/ioutil"
"os"

"github.com/flipez/rocket-lang/evaluator"
"github.com/flipez/rocket-lang/lexer"
"github.com/flipez/rocket-lang/object"
"github.com/flipez/rocket-lang/parser"
"github.com/flipez/rocket-lang/repl"
)

func main() {
repl.Start(os.Stdin, os.Stdout)
if len(os.Args) == 1 {
repl.Start(os.Stdin, os.Stdout)
} else {
file, err := ioutil.ReadFile(os.Args[1])
if err == nil {
env := object.NewEnvironment()
l := lexer.New(string(file))
p := parser.New(l)

program := p.ParseProgram()
if len(p.Errors()) > 0 {
printParserErrors(p.Errors())
return
}

evaluated := evaluator.Eval(program, env)
if evaluated != nil {
fmt.Println(evaluated.Inspect())
}
}
}
}

func printParserErrors(errors []string) {
fmt.Println("🔥 Great, you broke it!")
fmt.Println(" parser errors:")
for _, msg := range errors {
fmt.Printf("\t %s\n", msg)
}
}

0 comments on commit 1bcf34c

Please sign in to comment.