Пример #1
0
func parseAtom(tok lexer.Token) scheme.Sexpr {
	if isString(tok) {
		return scheme.StringFromString(string(tok.Val[1 : len(tok.Val)-1]))
	} else if isBoolean(tok) {
		return scheme.BooleanFromString(tok.Val)
	} else if isNumber(tok) {
		n, _ := strconv.ParseInt(tok.Val, 10, 64)
		return scheme.NumberFromInt(n)
	} else if isChar(tok) {
		n, _ := strconv.ParseInt(tok.Val[3:], 16, 64)
		return scheme.CharFromRune(rune(n))
	}
	return scheme.SymbolFromString(tok.Val)
}
Пример #2
0
func parseNext(ch chan lexer.Token, tok lexer.Token) (scheme.Sexpr, error) {
	switch tok {
	case _LPAREN:
		return parseCons(ch)
	case _RPAREN:
		return nil, parseError("unmatched )")
	case _PROTECT:
		s, err := parse(ch)
		if err != nil {
			return nil, err
		}
		return &scheme.Cons{scheme.SymbolFromString("quote"), &scheme.Cons{s, scheme.Nil}}, nil
	}
	return parseAtom(tok), nil
}
Пример #3
0
func Definitions() map[*scheme.Symbol]scheme.Sexpr {
	d := make(map[*scheme.Symbol]scheme.Sexpr)

	addFunction(d, "read", read)
	addFunction(d, "eval", primEval)
	addFunction(d, "print", print)
	addFunction(d, "display", display)

	addFunction(d, "boolean?", booleanCheck)
	addFunction(d, "pair?", pairCheck)
	addFunction(d, "symbol?", symbolCheck)
	addFunction(d, "number?", numberCheck)
	addFunction(d, "char?", charCheck)
	addFunction(d, "string?", stringCheck)
	addFunction(d, "procedure?", procCheck)
	addFunction(d, "null?", nullCheck)

	//Arithmetic
	addFunction(d, "+", plus)
	addFunction(d, "*", mul)
	addFunction(d, "-", sub)
	addFunction(d, "/", div)

	//Pairs
	addFunction(d, "cons", cons)
	addFunction(d, "car", car)
	addFunction(d, "cdr", cdr)
	addFunction(d, "set-car!", setcar)
	addFunction(d, "set-cdr!", setcdr)

	addPrim(d, "lambda", lambda)
	addPrim(d, "let", let)
	addPrim(d, "define", define)
	addPrim(d, "quote", quote)
	addPrim(d, "begin", begin)

	addPrim(d, "for", primFor)

	d[scheme.SymbolFromString("nil")] = scheme.Nil

	return d
}
Пример #4
0
func addPrim(d map[*scheme.Symbol]scheme.Sexpr, name string, p scheme.Proc) {
	d[scheme.SymbolFromString(name)] = scheme.CreatePrim(p, name)
}
Пример #5
0
func addFunction(d map[*scheme.Symbol]scheme.Sexpr, name string, p scheme.Proc) {
	d[scheme.SymbolFromString(name)] = scheme.CreateFunction(p, name)
}