added quote

master
Jordan Orelli 12 years ago
parent 29677e19e4
commit 107565dc04

@ -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)

@ -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)

@ -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

@ -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
}
Loading…
Cancel
Save