1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-11 13:55:55 +03:00

go: add step6_file

This commit is contained in:
Joel Martin 2014-10-06 22:27:28 -05:00
parent 17e1c5f9f4
commit ad95503cea
9 changed files with 261 additions and 5 deletions

View File

@ -257,6 +257,11 @@ Step Notes:
- in let*, do, and if:
- set ast and env and loop (no return)
- in fn* create Mal function type
- if compiled, update Makefile
- in apply, test if Mal function type:
- if so, generate new env from stored env, args and callee
params
- set ast to stored ast
- step6_file
@ -266,6 +271,23 @@ Step Notes:
- set *ARGV*
- if files on command line, use load-file to run first argument
using rest as arguments
- Details:
- cp step5_tco.EXT to step6_file.EXT
- if compiled update Makefile
- add eval to repl_env
- add empty *ARGV* list to repl_env
- in core.ns:
- implement slurp
- wrap printer.read-str as read-string
- implement load-file using rep
- test:
(load-file "../tests/inc.mal")
(inc3 10)
- implement command line execution
- test:
./step6_file ../tests/incA.mal
=>9
- implement comments in reader.EXT (ignore in tokenize)
- step7_quote
- add is_pair and quasiquote functions

View File

@ -4,13 +4,13 @@ export GOPATH := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
SOURCES_BASE = src/types/types.go src/reader/reader.go src/printer/printer.go \
src/env/env.go src/core/core.go
SOURCES_LISP = src/step5_tco/step5_tco.go
SOURCES_LISP = src/step6_file/step6_file.go
SOURCES = $(SOURCES_BASE) $(SOURCES_LISP)
#####################
SRCS = step0_repl.go step1_read_print.go step2_eval.go step3_env.go \
step4_if_fn_do.go step5_tco.go
step4_if_fn_do.go step5_tco.go step6_file.go
BINS = $(SRCS:%.go=%)
#####################
@ -20,8 +20,12 @@ all: $(BINS) mal
mal: $(word $(words $(BINS)),$(BINS))
cp $< $@
$(BINS): $(SOURCES_BASE) $(SOURCES_LISP)
go build $@
define dep_template
$(1): $(SOURCES_BASE) src/$(1)/$(1).go
go build $$@
endef
$(foreach b,$(BINS),$(eval $(call dep_template,$(b))))
clean:
rm -f $(BINS) mal

View File

@ -2,11 +2,13 @@ package core
import (
"errors"
"io/ioutil"
"fmt"
)
import (
. "types"
"reader"
"printer"
)
@ -31,6 +33,12 @@ func println(a []MalType) (MalType, error) {
return nil, nil
}
func slurp(a []MalType) (MalType, error) {
b, e := ioutil.ReadFile(a[0].(string))
if e != nil { return nil, e }
return string(b), nil
}
// Sequence functions
@ -62,6 +70,9 @@ var NS = map[string]MalType{
"str": func(a []MalType) (MalType, error) { return str(a) },
"prn": func(a []MalType) (MalType, error) { return prn(a) },
"println": func(a []MalType) (MalType, error) { return println(a) },
"read-string": func(a []MalType) (MalType, error) {
return reader.Read_str(a[0].(string)) },
"slurp": slurp,
"<": func(a []MalType) (MalType, error) {
return a[0].(int) < a[1].(int), nil },

View File

@ -49,6 +49,8 @@ func Pr_str(obj types.MalType, print_readably bool) string {
return "(fn* " +
Pr_str(tobj.Params, true) + " " +
Pr_str(tobj.Exp, true) + ")"
case func([]types.MalType)(types.MalType, error):
return fmt.Sprintf("<function %v>", obj)
default:
return fmt.Sprintf("%v", obj)
}

View File

@ -40,7 +40,7 @@ func tokenize (str string) []string {
results := make([]string, 0, 1)
re := regexp.MustCompile(`[\s,]*(~@|[\[\]{}()'~^@]|"(?:\\.|[^\\"])*"|;.*|[^\s\[\]{}('",;)]*)`)
for _, group := range re.FindAllStringSubmatch(str, -1) {
if group[1] == "" { continue }
if (group[1] == "") || (group[1][0] == ';') { continue }
results = append(results, group[1])
}
return results

View File

@ -156,9 +156,12 @@ func rep(str string) (MalType, error) {
}
func main() {
// core.go: defined using go
for k, v := range core.NS {
repl_env.Set(k, v)
}
// core.mal: defined using the language itself
rep("(def! not (fn* (a) (if a false true)))")
rdr := bufio.NewReader(os.Stdin);

View File

@ -164,9 +164,12 @@ func rep(str string) (MalType, error) {
}
func main() {
// core.go: defined using go
for k, v := range core.NS {
repl_env.Set(k, v)
}
// core.mal: defined using the language itself
rep("(def! not (fn* (a) (if a false true)))")
rdr := bufio.NewReader(os.Stdin);

View File

@ -0,0 +1,208 @@
package main
import (
"bufio"
//"io"
"fmt"
"os"
"strings"
"errors"
)
import (
. "types"
"reader"
"printer"
. "env"
"core"
)
// read
func READ(str string) (MalType, error) {
return reader.Read_str(str)
}
// eval
func eval_ast(ast MalType, env EnvType) (MalType, error) {
//fmt.Printf("eval_ast: %#v\n", ast)
if Symbol_Q(ast) {
return env.Get(ast.(Symbol).Val)
} else if List_Q(ast) {
lst := []MalType{}
for _, a := range ast.(List).Val {
exp, e := EVAL(a, env)
if e != nil { return nil, e }
lst = append(lst, exp)
}
return List{lst}, nil
} else if Vector_Q(ast) {
lst := []MalType{}
for _, a := range ast.(Vector).Val {
exp, e := EVAL(a, env)
if e != nil { return nil, e }
lst = append(lst, exp)
}
return Vector{lst}, nil
} else if Hash_Map_Q(ast) {
m := ast.(map[string]MalType)
new_hm := map[string]MalType{}
for k, v := range m {
ke, e1 := EVAL(k, env)
if e1 != nil { return nil, e1 }
if _, ok := ke.(string); !ok {
return nil, errors.New("non string hash-map key")
}
kv, e2 := EVAL(v, env)
if e2 != nil { return nil, e2 }
new_hm[ke.(string)] = kv
}
return new_hm, nil
} else {
return ast, nil
}
}
func EVAL(ast MalType, env EnvType) (MalType, error) {
for {
//fmt.Printf("EVAL: %v\n", printer.Pr_str(ast, true))
switch ast.(type) {
case List: // continue
default: return eval_ast(ast, env)
}
// apply list
a0 := ast.(List).Val[0]
var a1 MalType = nil; var a2 MalType = nil
switch len(ast.(List).Val) {
case 1:
a1 = nil; a2 = nil
case 2:
a1 = ast.(List).Val[1]; a2 = nil
default:
a1 = ast.(List).Val[1]; a2 = ast.(List).Val[2]
}
a0sym := "__<*fn*>__"
if Symbol_Q(a0) { a0sym = a0.(Symbol).Val }
switch a0sym {
case "def!":
res, e := EVAL(a2, env)
if e != nil { return nil, e }
return env.Set(a1.(Symbol).Val, res), nil
case "let*":
let_env, e := NewEnv(env, nil, nil)
if e != nil { return nil, e }
arr1, e := GetSlice(a1)
if e != nil { return nil, e }
for i := 0; i < len(arr1); i+=2 {
if !Symbol_Q(arr1[i]) {
return nil, errors.New("non-symbol bind value")
}
exp, e := EVAL(arr1[i+1], let_env)
if e != nil { return nil, e }
let_env.Set(arr1[i].(Symbol).Val, exp)
}
ast = a2
env = let_env
case "do":
lst := ast.(List).Val
_, e := eval_ast(List{lst[1:len(lst)-1]}, env)
if e != nil { return nil, e }
if len(lst) == 1 { return nil, nil }
ast = lst[len(lst)-1]
case "if":
cond, e := EVAL(a1, env)
if e != nil { return nil, e }
if cond == nil || cond == false {
if len(ast.(List).Val) >= 4 {
ast = ast.(List).Val[3]
} else {
return nil, nil
}
} else {
ast = a2
}
case "fn*":
fn := MalFunc{EVAL, a2, env, a1}
return fn, nil
default:
el, e := eval_ast(ast, env)
if e != nil { return nil, e }
f := el.(List).Val[0]
if MalFunc_Q(f) {
fn := f.(MalFunc)
ast = fn.Exp
env, e = NewEnv(fn.Env, fn.Params.(List).Val, el.(List).Val[1:])
if e != nil { return nil, e }
} else {
fn, ok := f.(func([]MalType)(MalType, error))
if !ok { return nil, errors.New("attempt to call non-function") }
return fn(el.(List).Val[1:])
}
}
} // TCO loop
}
// print
func PRINT(exp MalType) (string, error) {
return printer.Pr_str(exp, true), nil
}
var repl_env, _ = NewEnv(nil, nil, nil)
// repl
func rep(str string) (MalType, error) {
var exp MalType
var res string
var e error
if exp, e = READ(str); e != nil { return nil, e }
if exp, e = EVAL(exp, repl_env); e != nil { return nil, e }
if res, e = PRINT(exp); e != nil { return nil, e }
return res, nil
}
func main() {
// core.go: defined using go
for k, v := range core.NS {
repl_env.Set(k, v)
}
repl_env.Set("eval", func(a []MalType) (MalType, error) {
return EVAL(a[0], repl_env) })
repl_env.Set("*ARGV*", List{})
// core.mal: defined using the language itself
rep("(def! not (fn* (a) (if a false true)))")
rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
// called with mal script to load and eval
if len(os.Args) > 1 {
args := make([]MalType, 0, len(os.Args)-2)
for _,a := range os.Args[2:] {
args = append(args, a)
}
repl_env.Set("*ARGV*", List{args})
rep("(load-file \"" + os.Args[1] + "\")")
os.Exit(0)
}
rdr := bufio.NewReader(os.Stdin);
// repl loop
for {
fmt.Print("user> ");
text, err := rdr.ReadString('\n');
text = strings.TrimRight(text, "\n");
if (err != nil) {
return
}
var out MalType
var e error
if out, e = rep(text); e != nil {
if e.Error() == "<empty line>" { continue }
fmt.Printf("Error: %v\n", e)
continue
}
fmt.Printf("%v\n", out)
}
}

3
tests/incA.mal Normal file
View File

@ -0,0 +1,3 @@
(def! inc4 (fn* (a) (+ 4 a)))
(prn (inc4 5))