From 107565dc04129eac0f4917b94740cc7a0c47725e Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 20 Oct 2012 20:09:42 -0400 Subject: [PATCH] added quote --- input.lisp | 42 ------------------------------------------ input.scm | 30 ++++++++++++++++++++++++++++++ skeam.go | 5 +++++ special.go | 24 ++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 42 deletions(-) delete mode 100644 input.lisp create mode 100644 input.scm create mode 100644 special.go diff --git a/input.lisp b/input.lisp deleted file mode 100644 index 5e3ae9f..0000000 --- a/input.lisp +++ /dev/null @@ -1,42 +0,0 @@ -(+ 1 (+ 1 1) (dave (sam 1))) - -; alright I have comments now!!!! -; woooohooo!!!! -; nice nice nice - -(+ 1 - 22.3 - a - ;here comes a comment - 3. - (one two three) - "this is a string" - 4.0 - ((one two) three) - abøne - (dave - 1 - "here's an escaped quote: \" how neat!!!" - 2 - "and here;'s an escaped \\, sweet!" - albert-camus - 3 - (sam 3 2 2))) - - - -(begin (set! x 1) - (set! x (+ x 1)) - (* x 2)) - -; ------------------------------------------------------------------------------ -; the following stuff comes directly from the norvig essay, instead of being -; contrived lexer tests. -; ------------------------------------------------------------------------------ - -; define a function and then execute it -(begin (define r 3) (* 3.141592653 (* r r))) - -; same thing, alternative form without "begin" -(define area (lambda (r) (* 3.141592653 (* r r)))) -(area 3) diff --git a/input.scm b/input.scm new file mode 100644 index 0000000..fddaea4 --- /dev/null +++ b/input.scm @@ -0,0 +1,30 @@ +; ------------------------------------------------------------------------------ +; literals +; ------------------------------------------------------------------------------ + +; integer +1 + +; float +3.14 + +; string +"jordan" + +; ------------------------------------------------------------------------------ +; basic math +; ------------------------------------------------------------------------------ + +(+ 1 1) +(- 1 1) +(* 1 1) +(/ 1 1) + +; ------------------------------------------------------------------------------ +; grammar +; ------------------------------------------------------------------------------ + +(define x 5) +x + +(quote 1 2 3) diff --git a/skeam.go b/skeam.go index dfcb65d..b3cc39a 100644 --- a/skeam.go +++ b/skeam.go @@ -14,6 +14,10 @@ var DEBUG = false type sexp []interface{} +func (s sexp) String() string { + return "(" + fmt.Sprint(s...) + ")" +} + type symbol string var universe = &environment{ @@ -22,6 +26,7 @@ var universe = &environment{ "*": proc(multiplication), "/": proc(division), "define": special(define), + "quote": special(quote), } // parses the string lexeme into a value that can be eval'd diff --git a/special.go b/special.go new file mode 100644 index 0000000..42e14fc --- /dev/null +++ b/special.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "reflect" +) + +type special func(*environment, ...interface{}) (interface{}, error) + +func define(env *environment, args ...interface{}) (interface{}, error) { + if len(args) != 2 { + return nil, fmt.Errorf(`received %d arguments in *define*, expected exactly 2`, len(args)) + } + s, ok := args[0].(symbol) + if !ok { + return nil, fmt.Errorf(`first argument to *define* must be symbol, received %v`, reflect.TypeOf(args[0])) + } + env.set(s, args[1]) + return nil, nil +} + +func quote(_ *environment, args ...interface{}) (interface{}, error) { + return sexp(args), nil +}