Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Using a REPL written in lisp

  • Loading branch information...
commit b405c171bc42d4e6acb2f26072d830979b38c02a 1 parent a5fe044
@bytbox authored
View
1  .gitignore
@@ -7,3 +7,4 @@ kakapo
packages.go
lisp/scanpkgs/scanpkgs
*.a
+repl.go
View
8 Makefile
@@ -1,9 +1,10 @@
include ${GOROOT}/src/Make.inc
TARG = kakapo
-GOFILES = kakapo.go
+GOFILES = kakapo.go repl.go
PREREQ = lisp
-CLEANFILES = _go_.${O} ${TARG} lisp.a
+CLEANFILES = _go_.${O} ${TARG} lisp.a repl.go
+TXT2GO = ./txt2go.sh
${TARG}: _go_.$O
${LD} ${LDIMPORTS} -o $@ _go_.$O
@@ -11,6 +12,9 @@ ${TARG}: _go_.$O
_go_.${O}: ${GOFILES} ${PREREQ}
$(GC) $(GCFLAGS) $(GCIMPORTS) -o $@ $(GOFILES)
+repl.go: repl.lsp
+ ${TXT2GO} repl < repl.lsp > $@
+
lisp:
make -Clisp
cp lisp/_obj/lisp.a .
View
4 kakapo.go
@@ -2,12 +2,12 @@ package main
import (
"flag"
- "os"
+ "strings"
. "./lisp"
)
func main() {
flag.Parse()
- EvalFrom(os.Stdin)
+ EvalFrom(strings.NewReader(repl))
}
View
1  lisp/builtins.go
@@ -13,6 +13,7 @@ func init() {
globalData := map[sym]sexpr{
// Primitives
"if": primitive(primitiveIf),
+ "for": primitive(primitiveFor),
"lambda": primitive(primitiveLambda),
"let": primitive(primitiveLet),
"define": primitive(primitiveDefine),
View
4 lisp/interpret.go
@@ -1,7 +1,6 @@
package lisp
import (
- "fmt"
"io"
)
@@ -13,8 +12,7 @@ func EvalFrom(ior io.Reader) {
r := GetRuneScanner(ior)
e, err := parse(r)
for err == nil {
- v := eval(global, e)
- fmt.Println(asString(v))
+ eval(global, e)
e, err = parse(r)
}
}
View
16 lisp/primitives.go
@@ -17,6 +17,22 @@ func primitiveIf(sc *scope, ss []sexpr) sexpr {
return Nil
}
+// (for cond expr)
+func primitiveFor(sc *scope, ss []sexpr) sexpr {
+ if len(ss) != 2 {
+ panic("Invalid number of arguments")
+ }
+ cond := ss[0]
+ expr := ss[1]
+ val := Nil
+ cv := eval(sc, cond)
+ for cv != nil {
+ val = eval(sc, expr)
+ cv = eval(sc, cond)
+ }
+ return val
+}
+
// (lambda (arg1 ...) expr)
func primitiveLambda(sc *scope, ss []sexpr) sexpr {
if len(ss) != 2 {
View
3  repl.lsp
@@ -0,0 +1,3 @@
+; Read-Eval-Print Loop for Kakapo
+
+(for 1 (print (read)))
View
10 txt2go.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+ECHO=/bin/echo
+
+$ECHO "package main"
+$ECHO
+$ECHO -n "var $1 = \`"
+cat
+$ECHO "\`"
+
Please sign in to comment.
Something went wrong with that request. Please try again.