Skip to content

Commit

Permalink
Merge pull request #18 from GraHms/develop
Browse files Browse the repository at this point in the history
feature: include REPL
  • Loading branch information
GraHms authored Oct 8, 2023
2 parents 7ee8d7c + 36a79a5 commit ec8a41f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 32 deletions.
31 changes: 31 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package commands

import (
"os"
"fmt"
"github.com/grahms/samoralang/evaluator"

Check failure on line 6 in commands/commands.go

View workflow job for this annotation

GitHub Actions / build

cannot find package "github.com/grahms/samoralang/evaluator" in any of:
"github.com/grahms/samoralang/lexer"

Check failure on line 7 in commands/commands.go

View workflow job for this annotation

GitHub Actions / build

cannot find package "github.com/grahms/samoralang/lexer" in any of:
"github.com/grahms/samoralang/object"

Check failure on line 8 in commands/commands.go

View workflow job for this annotation

GitHub Actions / build

cannot find package "github.com/grahms/samoralang/object" in any of:
"github.com/grahms/samoralang/parser"

Check failure on line 9 in commands/commands.go

View workflow job for this annotation

GitHub Actions / build

cannot find package "github.com/grahms/samoralang/parser" in any of:
)

var Commands = map[string]func([]byte) {
"run": func(input []byte) {
Execute(string(input))
},
}

func Execute(input string) int {
env := object.NewEnvironment()
l := lexer.New(input)

p := parser.New(l)

program := p.ParseProgram()
if len(p.Errors()) != 0 {
fmt.Printf("Error parsing: %s\n", p.Errors())
os.Exit(1)
}
_ = evaluator.Eval(program, env)
return 0
}
13 changes: 13 additions & 0 deletions commands/start_repl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package commands

import (
"os"
"bufio"
"github.com/grahms/samoralang/repl"

Check failure on line 6 in commands/start_repl.go

View workflow job for this annotation

GitHub Actions / build

cannot find package "github.com/grahms/samoralang/repl" in any of:
)

func StartREPL() {
in := bufio.NewReader(os.Stdin)
out := os.Stdout
repl.Start(in, out)
}
47 changes: 17 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,33 @@ package main
import (
"flag"
"fmt"
"github.com/grahms/samoralang/evaluator"
"github.com/grahms/samoralang/lexer"
"github.com/grahms/samoralang/object"
"github.com/grahms/samoralang/parser"
"io"
"github.com/grahms/samoralang/commands"

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / build

cannot find package "github.com/grahms/samoralang/commands" in any of:
"os"
)

func main() {

flag.Parse()

var input []byte
var err error

if len(flag.Args()) > 0 {
input, err = os.ReadFile(os.Args[1])
} else {
input, err = io.ReadAll(os.Stdin)
switch len(flag.Args()) {
case 0:
commands.StartREPL()
case 1:
handleCommand("run", 1)
default:
handleCommand(os.Args[1], 2)
}

if err != nil {
fmt.Printf("Error reading: %s\n", err.Error())
}

Execute(string(input))
}

func Execute(input string) int {
func handleCommand(command string, arg_n int) {
input_file := os.Args[arg_n]
input, err := os.ReadFile(input_file)

env := object.NewEnvironment()
l := lexer.New(input)

p := parser.New(l)

program := p.ParseProgram()
if len(p.Errors()) != 0 {
fmt.Printf("Error parsing: %s\n", p.Errors())
os.Exit(1)
if err != nil {
pwd, _ := os.Getwd()
fmt.Printf("Error: can't open %s/%s\n", pwd, input_file)
return
}
_ = evaluator.Eval(program, env)
return 0

commands.Commands[command](input);
}
3 changes: 1 addition & 2 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"io"
)

const PROMPT = ">> "
const PROMPT = ">>> "

func Start(in io.Reader, out io.Writer) {
scanner := bufio.NewScanner(in)
Expand All @@ -32,7 +32,6 @@ func Start(in io.Reader, out io.Writer) {
}
evaluated := evaluator.Eval(program, env)
if evaluated != nil {
_, _ = io.WriteString(out, evaluated.Inspect())
_, _ = io.WriteString(out, "\n")
}

Expand Down

0 comments on commit ec8a41f

Please sign in to comment.