Пример #1
0
func main() {
	// core.go: defined using go
	for k, v := range core.NS {
		repl_env.Set(Symbol{k}, Func{v.(func([]MalType) (MalType, error)), nil})
	}

	// core.mal: defined using the language itself
	rep("(def! not (fn* (a) (if a false true)))")

	// repl loop
	for {
		text, err := readline.Readline("user> ")
		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)
	}
}
Пример #2
0
func main() {
	repl_env.Set(Symbol{"+"}, func(a []MalType) (MalType, error) {
		return a[0].(int) + a[1].(int), nil
	})
	repl_env.Set(Symbol{"-"}, func(a []MalType) (MalType, error) {
		return a[0].(int) - a[1].(int), nil
	})
	repl_env.Set(Symbol{"*"}, func(a []MalType) (MalType, error) {
		return a[0].(int) * a[1].(int), nil
	})
	repl_env.Set(Symbol{"/"}, func(a []MalType) (MalType, error) {
		return a[0].(int) / a[1].(int), nil
	})

	// repl loop
	for {
		text, err := readline.Readline("user> ")
		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
0
func main() {
	// repl loop
	for {
		text, err := readline.Readline("user> ")
		text = strings.TrimRight(text, "\n")
		if err != nil {
			return
		}
		fmt.Println(rep(text))
	}
}
Пример #4
0
func main() {
	// core.go: defined using go
	for k, v := range core.NS {
		repl_env.Set(Symbol{k}, Func{v.(func([]MalType) (MalType, error)), nil})
	}
	repl_env.Set(Symbol{"eval"}, Func{func(a []MalType) (MalType, error) {
		return EVAL(a[0], repl_env)
	}, nil})
	repl_env.Set(Symbol{"*ARGV*"}, List{})

	// core.mal: defined using the language itself
	rep("(def! *host-language* \"go\")")
	rep("(def! not (fn* (a) (if a false true)))")
	rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
	rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))")
	rep("(def! *gensym-counter* (atom 0))")
	rep("(def! gensym (fn* [] (symbol (str \"G__\" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))")
	rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))")

	// 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(Symbol{"*ARGV*"}, List{args, nil})
		if _, e := rep("(load-file \"" + os.Args[1] + "\")"); e != nil {
			fmt.Printf("Error: %v\n", e)
			os.Exit(1)
		}
		os.Exit(0)
	}

	// repl loop
	rep("(println (str \"Mal [\" *host-language* \"]\"))")
	for {
		text, err := readline.Readline("user> ")
		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)
	}
}
Пример #5
0
func main() {
	// core.go: defined using go
	for k, v := range core.NS {
		repl_env.Set(Symbol{k}, Func{v.(func([]MalType) (MalType, error)), nil})
	}
	repl_env.Set(Symbol{"eval"}, Func{func(a []MalType) (MalType, error) {
		return EVAL(a[0], repl_env)
	}, nil})
	repl_env.Set(Symbol{"*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(Symbol{"*ARGV*"}, List{args, nil})
		if _, e := rep("(load-file \"" + os.Args[1] + "\")"); e != nil {
			fmt.Printf("Error: %v\n", e)
			os.Exit(1)
		}
		os.Exit(0)
	}

	// repl loop
	for {
		text, err := readline.Readline("user> ")
		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)
	}
}
Пример #6
0
func main() {
	// repl loop
	for {
		text, err := readline.Readline("user> ")
		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)
	}
}
Пример #7
0
		}
	},
	"keyword?": func(a []MalType) (MalType, error) {
		return Keyword_Q(a[0]), nil
	},

	"pr-str":  func(a []MalType) (MalType, error) { return pr_str(a) },
	"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,
	"readline": func(a []MalType) (MalType, error) {
		return readline.Readline(a[0].(string))
	},

	"<": func(a []MalType) (MalType, error) {
		return a[0].(int) < a[1].(int), nil
	},
	"<=": func(a []MalType) (MalType, error) {
		return a[0].(int) <= a[1].(int), nil
	},
	">": func(a []MalType) (MalType, error) {
		return a[0].(int) > a[1].(int), nil
	},
	">=": func(a []MalType) (MalType, error) {
		return a[0].(int) >= a[1].(int), nil
	},
	"+": func(a []MalType) (MalType, error) {