Files
kraken/k_prime.krak

914 lines
32 KiB
Plaintext
Raw Normal View History

import mem:*
import io:*
import str:*
import vec:*
import vec_literals:*
import util:*
import map:*
2020-05-10 00:17:30 -04:00
import rc:*
import fungll:*
2020-06-28 19:17:09 -04:00
adt KPValue_int {
True, // 101 1 0
False, // 100 1 0
Env: *KPEnv, // 011 1 0
Combiner: KPCombiner, // 010 1 0
BuiltinCombiner: KPBuiltinCombiner, // ''' ' '
String: str, // 001 1 0
Symbol: str, // 000 1 0
Int: int, // 0 0
2020-06-28 19:17:09 -04:00
Vector: rc<vec<KPValue>>, //<10sizebits> 1
// same encoding, but encodes 0-length null ptr
Nil // 00000000000 1
}
2020-06-28 19:17:09 -04:00
obj KPEnv (Object) {
var data: map<str, KPValue>
var outer: *KPEnv
fun construct(): *KPEnv {
return construct(null<KPEnv>())
}
fun construct(outer: *KPEnv): *KPEnv {
data.construct()
this->outer = outer
return this
}
fun copy_construct(old: *KPEnv): void {
data.copy_construct(&old->data)
outer = old->outer
}
fun destruct(): void {
data.destruct()
outer = null<KPEnv>()
}
fun operator=(other:ref KPEnv):void {
destruct()
copy_construct(&other)
}
fun set(key: str, val: KPValue) {
data.set(key, val)
}
fun remove(key: str) {
data.remove(key)
}
fun find(key: str): *KPEnv {
if (data.contains_key(key)) {
return this
} else if (outer != null<KPEnv>()) {
return outer->find(key)
} else {
return null<KPEnv>()
}
}
fun get(key: str): KPResult {
var env = find(key)
if (env != null<KPEnv>()) {
return KPResult::Ok(env->data.get(key))
} else {
println(key + "wasn't found in:")
println(to_string())
2020-06-28 19:17:09 -04:00
return KPResult::Err(kpString(str("'") + key + "' not found"))
}
}
fun to_string(): str {
var to_ret = str()
to_string(str("\t"), to_ret)
return to_ret
}
fun to_string(tabs: ref str, s: ref str) {
for (var i = 0; i < data.keys.size; i++;) {
2020-06-28 23:28:15 -04:00
s += tabs + data.keys[i] + ": " + pr_str(data.values[i], true) + "\n"
/*s += tabs + data.keys[i] + "\n"*/
2020-06-28 19:17:09 -04:00
}
if outer != null<KPEnv>() {
outer->to_string(tabs + "\t", s)
}
}
}
2020-06-28 19:17:09 -04:00
obj KPBuiltinCombiner (Object) {
var name: str
var fp: fun(vec<KPValue>, *KPEnv): KPResult
fun construct(name: ref str, fp: fun(vec<KPValue>, *KPEnv): KPResult): *KPBuiltinCombiner {
this->name.copy_construct(&name)
this->fp = fp
return this
}
fun copy_construct(old: *KPBuiltinCombiner): void {
this->fp = old->fp
this->name.copy_construct(&old->name)
}
fun destruct(): void {
this->name.destruct()
}
fun operator=(other:ref KPBuiltinCombiner):void {
destruct()
copy_construct(&other)
}
fun operator==(other: ref KPBuiltinCombiner):bool {
return false
}
fun call(params: vec<KPValue>, dynamic_env: KPValue): KPResult {
if !dynamic_env.is_env() {
return KPResult::Err(kpString(pr_str(dynamic_env, true) + " is not an env"))
}
return fp(params, dynamic_env.get_env())
}
}
2020-06-28 19:17:09 -04:00
fun make_builtin_combiner(name: str, f: fun(vec<KPValue>, *KPEnv): KPResult): KPValue {
var to_ret.construct(name, f): KPBuiltinCombiner
return nmMV(KPValue_int::BuiltinCombiner(to_ret))
}
2020-06-28 19:17:09 -04:00
obj KPCombiner (Object) {
var env: *KPEnv
var dynamic_env_name: str
var parameters: vec<str>
var is_variadic: bool
var body: *KPValue
fun construct(env: *KPEnv, dynamic_env_name: str, parameters: vec<str>, is_variadic: bool, body: KPValue): *KPCombiner {
this->env = env
this->dynamic_env_name.copy_construct(&dynamic_env_name)
this->parameters.copy_construct(&parameters)
this->is_variadic = is_variadic
this->body = new<KPValue>()
this->body->copy_construct(&body)
return this
}
fun copy_construct(old: *KPCombiner): void {
this->env = old->env
this->dynamic_env_name.copy_construct(&old->dynamic_env_name)
this->parameters.copy_construct(&old->parameters)
this->is_variadic = old->is_variadic
this->body = new<KPValue>()
this->body->copy_construct(old->body)
}
fun destruct(): void {
this->env = null<KPEnv>()
dynamic_env_name.destruct()
parameters.destruct()
delete(body)
body = null<KPValue>()
}
fun operator=(other:ref KPCombiner):void {
destruct()
copy_construct(&other)
}
fun operator==(other: ref KPCombiner):bool {
// not sure about env
return env == other.env && dynamic_env_name == other.dynamic_env_name && parameters == other.parameters && is_variadic == other.is_variadic && body->equals(*other.body)
}
// no call b/c need to do in EVAL for TCO
fun prep_call(params: ref vec<KPValue>, dynamic_env: KPValue): pair<*KPEnv, KPResult> {
// tco
if (!is_variadic && parameters.size != params.size) || (is_variadic && parameters.size > params.size + 1) {
return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("combiner called with the wrong number of parameters: ") + params.size + " but expecting " + parameters.size + ": [ " + str(",").join(parameters) + "], was: " + pr_str(kpVector(params), true) + ", function body is " + pr_str(*body, true))))
}
var new_env = new<KPEnv>()->construct(env)
for (var i = 0; i < parameters.size; i++;) {
if is_variadic && i == parameters.size - 1 {
new_env->set(parameters[i], kpVector(params.slice(i, -1)))
} else {
new_env->set(parameters[i], params[i])
}
}
new_env->set(dynamic_env_name, dynamic_env)
2020-06-28 23:28:15 -04:00
/*println("Calling with\n" + new_env->to_string())*/
2020-06-28 19:17:09 -04:00
return make_pair(new_env, KPResult::Ok(*body))
}
}
2020-06-28 19:17:09 -04:00
obj KPValue (Object) {
var internal: KPValue_int
var meta: *KPValue
fun construct(): *KPValue {
internal.copy_construct(&KPValue_int::Nil());
meta = null<KPValue>()
}
2020-06-28 19:17:09 -04:00
fun construct(i: ref KPValue_int, m: *KPValue): *KPValue {
internal.copy_construct(&i);
meta = m
}
2020-06-28 19:17:09 -04:00
fun copy_construct(other: *KPValue): void {
internal.copy_construct(&other->internal);
/*meta = other->meta*/
2020-06-28 19:17:09 -04:00
if other->meta != null<KPValue>() {
meta = new<KPValue>()
meta->copy_construct(other->meta)
} else {
2020-06-28 19:17:09 -04:00
meta = null<KPValue>()
}
}
2020-06-28 19:17:09 -04:00
fun operator=(other: ref KPValue): void {
destruct()
copy_construct(&other)
}
fun destruct(): void {
2020-06-28 19:17:09 -04:00
if meta != null<KPValue>()
delete(meta)
internal.destruct()
}
2020-06-28 19:17:09 -04:00
fun equals(other: ref KPValue): bool {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::Vector(d) { match (other.internal) {
KPValue_int::Vector(db) {
if d.get().size != db.get().size {
return false
}
for (var i = 0; i < d.get().size; i++;) {
if !d.get()[i].equals(db.get()[i]) {
return false
}
}
return true
}
} }
2020-06-28 19:17:09 -04:00
KPValue_int::String(d) { match (other.internal) { KPValue_int::String(db) { return d == db; } } }
KPValue_int::Int(d) { match (other.internal) { KPValue_int::Int(db) { return d == db; } } }
KPValue_int::Symbol(d) { match (other.internal) { KPValue_int::Symbol(db) { return d == db; } } }
KPValue_int::Combiner(d){ match (other.internal) { KPValue_int::Combiner(db) { return d == db; } } }
KPValue_int::BuiltinCombiner(d) { match (other.internal) { KPValue_int::BuiltinCombiner(db) { return d == db; } } }
KPValue_int::Env(e) { match (other.internal) { KPValue_int::Env(eb) { return e == eb; } } }
KPValue_int::True() { match (other.internal) { KPValue_int::True() { return true; } } }
KPValue_int::False() { match (other.internal) { KPValue_int::False() { return true; } } }
KPValue_int::Nil() { match (other.internal) { KPValue_int::Nil() { return true; } } }
}
return false
}
2020-06-28 19:17:09 -04:00
fun deep_clone(): KPValue {
2020-05-10 00:17:30 -04:00
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::Vector(v) {
return kpVector(v.get())
}
KPValue_int::Env(e) {
var newenv = new<KPEnv>()
newenv->copy_construct(e)
return kpEnv(e)
2020-05-10 00:17:30 -04:00
}
}
return *this
}
2020-06-28 19:17:09 -04:00
fun is_combiner(): bool {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::Combiner(f) {
return true
}
KPValue_int::BuiltinCombiner(f) {
return true
}
2020-06-28 19:17:09 -04:00
}
return false
}
fun is_env(): bool {
match (internal) {
KPValue_int::Env(e) {
return true
}
}
return false
}
2020-06-28 19:17:09 -04:00
fun get_env(): *KPEnv {
match (internal) {
KPValue_int::Env(e) {
return e
}
}
error("Tried to get env on not an env" + pr_str(*this, true))
}
fun is_vector(): bool {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::Vector(v) {
return true
}
}
return false
}
2020-06-28 19:17:09 -04:00
fun get_vector_rc(): rc<vec<KPValue>> {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::Vector(v) {
return v
}
}
error("Tried to get vec on not a vec" + pr_str(*this, true))
}
fun is_symbol(): bool {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::Symbol(s) {
return true
}
}
return false
}
fun is_symbol(text: *char): bool {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::Symbol(s) {
return s == text
}
}
return false
}
fun get_symbol_text(): str {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::Symbol(s) {
return s
}
}
error("get_symbol_text on not symbol")
}
fun is_string(): bool {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::String(s) {
return true
}
}
return false
}
fun get_string(): str {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::String(s) {
return s
}
}
error("get_string on not a string")
}
fun is_int(): bool {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::Int(i) {
return true
}
}
return false
}
fun get_int(): int {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::Int(i) {
return i
}
}
error("get_int on not an int")
}
fun is_nil(): bool {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::Nil() {
return true
}
}
return false
}
fun is_truthy(): bool {
match (internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::False() {
return false
}
2020-06-28 19:17:09 -04:00
KPValue_int::Nil() {
return false
}
}
return true
}
fun is_pair(): bool {
return is_vector() && get_vector_rc().get().size > 0
}
}
2020-06-28 19:17:09 -04:00
fun nmMV(i: ref KPValue_int): KPValue {
var to_ret.construct(i, null<KPValue>()): KPValue
return to_ret
}
fun kpTrue(): KPValue {
return nmMV(KPValue_int::True())
}
fun kpFalse(): KPValue {
return nmMV(KPValue_int::False())
}
fun kpBool(b: bool): KPValue {
2020-06-28 19:17:09 -04:00
if b {
return nmMV(KPValue_int::True())
} else {
return nmMV(KPValue_int::False())
}
}
fun kpString(s: ref str): KPValue {
return nmMV(KPValue_int::String(s))
}
fun kpSymbol(s: ref str): KPValue {
return nmMV(KPValue_int::Symbol(s))
}
fun kpInt(i: int): KPValue {
return nmMV(KPValue_int::Int(i))
}
fun kpVector(v: ref vec<KPValue>): KPValue {
var rcd.construct(v): rc<vec<KPValue>>
return nmMV(KPValue_int::Vector(rcd))
}
fun kpNil(): KPValue {
return nmMV(KPValue_int::Nil())
}
fun kpEnv(e: *KPEnv): KPValue {
return nmMV(KPValue_int::Env(e))
}
fun read_str(grammar: ref Grammer<KPResult,KPValue>, s: str): pair<int, KPResult> {
2020-05-12 21:30:37 -04:00
var BSR = fungll(grammar, grammar.start_symbol, s)
var longest = -1
for (var i = 0; i < BSR.data.size; i++;) {
2020-05-12 21:30:37 -04:00
if BSR.data[i].nonterminal == grammar.start_symbol && BSR.data[i].left == 0 && BSR.data[i].idx_into_rule == grammar.nonterminals[(-1*BSR.data[i].nonterminal)-1][BSR.data[i].rule_idx].size {
longest = BSR.data[i].right
}
}
if longest >= 0 {
2020-05-12 21:30:37 -04:00
return make_pair(longest, grammar.eval_BSR(s.slice(0, longest), BSR))
} else {
println("trying to parse: " + s)
println(str("length of BSR is: ") + BSR.size())
for (var i = 0; i < BSR.data.size; i++;) {
2020-05-12 21:30:37 -04:00
println(str() + i + ": " + grammar.to_string(BSR.data[i]))
}
println("Parse failed")
2020-06-28 19:17:09 -04:00
return make_pair(-1, KPResult::Err(kpString(str("failed to parse"))))
}
}
2020-06-28 19:17:09 -04:00
adt KPResult {
Ok: KPValue,
Err: KPValue
}
fun is_err(r: KPResult): bool {
match (r) {
KPResult::Err(e) {
return true
}
}
2020-06-28 19:17:09 -04:00
return false
}
fun get_err(r: KPResult): KPValue {
match (r) {
KPResult::Err(e) {
return e
}
}
2020-06-28 19:17:09 -04:00
error("get-err-not-error")
}
fun get_value(r: KPResult): KPValue {
match (r) {
KPResult::Ok(v) {
return v
}
}
2020-06-28 19:17:09 -04:00
error("get-value-is-error")
}
2020-06-28 19:17:09 -04:00
fun pr_str(v: KPValue, print_readably: bool): str {
match (v.internal) {
KPValue_int::Vector(l) {
var to_ret = str("( ")
for (var i = 0; i < l.get().size; i++;) {
if (i != 0) {
to_ret += " "
}
2020-06-28 19:17:09 -04:00
to_ret += pr_str(l.get()[i], print_readably)
}
return to_ret + " )"
}
KPValue_int::Int(i) {
return to_string(i)
}
KPValue_int::String(s) {
if print_readably {
var to_ret = str("\"") //"
for (var i = 0; i < s.length(); i++;) {
if s[i] == '\n' {
to_ret += '\\'
to_ret += 'n'
} else if s[i] == '\\' || s[i] == '"' {
to_ret += '\\'
to_ret += s[i]
} else {
2020-06-28 19:17:09 -04:00
to_ret += s[i]
}
}
2020-06-28 19:17:09 -04:00
return to_ret + "\"" //"
} else {
return s
}
}
KPValue_int::Symbol(s) {
if print_readably {
return "'" + s
} else {
return s
}
}
2020-06-28 19:17:09 -04:00
KPValue_int::BuiltinCombiner(f) {
return "builtin_combiner_" + f.name
2020-06-28 19:17:09 -04:00
}
KPValue_int::Combiner(f) {
return str("combiner")
}
KPValue_int::Env(e) {
return str("environment")
}
KPValue_int::True() {
return str("true")
}
KPValue_int::False() {
return str("false")
}
KPValue_int::Nil() {
return str("nil")
}
}
2020-06-28 19:17:09 -04:00
error("can't print")
}
2020-06-28 19:17:09 -04:00
fun READ(grammar: ref Grammer<KPResult, KPValue>, s: str): KPResult {
var to_ret = read_str(grammar, s)
if to_ret.first != s.length() {
if is_err(to_ret.second) {
return to_ret.second
} else {
2020-06-28 19:17:09 -04:00
println("parsed to var: " + pr_str(get_value(to_ret.second), true))
}
2020-06-28 19:17:09 -04:00
error("parsed some, but not all")
}
2020-06-28 19:17:09 -04:00
return to_ret.second
}
2020-06-28 19:17:09 -04:00
fun EVAL(env: *KPEnv, ast: KPValue): KPResult {
// for tco
while (true) {
match (ast.internal) {
KPValue_int::Vector(l) {
if (l.get().size == 0) {
return KPResult::Err(kpString(str("Eval a zero length vector")))
} else {
var combiner = EVAL(env, l.get()[0])
if is_err(combiner) {
return combiner
}
/*println("About to call combiner evaled from " + pr_str(l.get()[0], true))*/
2020-06-28 19:17:09 -04:00
match (get_value(combiner).internal) {
KPValue_int::BuiltinCombiner(f) {
return f.call(l.get().slice(1,-1), kpEnv(env))
}
KPValue_int::Combiner(f) {
var call_pair = f.prep_call(l.get().slice(1, -1), kpEnv(env))
if is_err(call_pair.second) {
return call_pair.second
}
env = call_pair.first
ast = get_value(call_pair.second)
continue
}
}
return KPResult::Err(kpString(str("trying to call not a combiner: ") + pr_str(l.get()[0], true)))
}
}
KPValue_int::Symbol(s) {
return env->get(s)
}
}
2020-06-28 19:17:09 -04:00
// everything else is self-evaluating
return KPResult::Ok(ast)
}
2020-06-28 19:17:09 -04:00
}
fun rep(grammar: ref Grammer<KPResult, KPValue>, env: *KPEnv, a: str): str {
var read = READ(grammar, a)
if is_err(read) {
return pr_str(get_err(read), true)
} else {
var evaled = EVAL(env, get_value(read))
if is_err(evaled) {
return str("Exception: ") + pr_str(get_err(evaled), true)
} else {
2020-06-28 19:17:09 -04:00
return pr_str(get_value(evaled), true)
}
}
}
2020-06-28 19:17:09 -04:00
fun function_call(f: KPValue, params: ref vec<KPValue>, env: KPValue): KPResult {
match (f.internal) {
2020-06-28 19:17:09 -04:00
KPValue_int::BuiltinCombiner(f) {
return f.call(params, env)
}
2020-06-28 19:17:09 -04:00
KPValue_int::Combiner(f) {
var call_pair = f.prep_call(params, env)
if is_err(call_pair.second) {
return call_pair.second
}
return EVAL(call_pair.first, get_value(call_pair.second))
}
}
2020-06-28 19:17:09 -04:00
return KPResult::Err(kpString(str("trying to apply not a combiner: ") + pr_str(f ,true)))
}
2020-06-28 19:17:09 -04:00
fun print_wrapper(params: ref vec<KPValue>, sep: *char, print_readably: bool): str {
var to_ret = str()
for (var i = 0; i < params.size; i++;) {
to_ret += pr_str(params[i], print_readably)
if i != params.size-1 {
to_ret += sep
}
}
return to_ret
}
fun main(argc: int, argv: **char): int {
2020-06-28 19:17:09 -04:00
var grammar.construct(): Grammer<KPResult, KPValue>
2020-06-28 19:17:09 -04:00
var ret_nil_term: fun(ref KPValue, ref str, int, int): KPResult = fun(_: ref KPValue, input: ref str, l: int, r: int): KPResult { return KPResult::Ok(kpNil()); }
var ret_nil_sym: fun(ref KPValue, ref vec<KPResult>): KPResult = fun(_: ref KPValue, x: ref vec<KPResult>): KPResult { return KPResult::Ok(kpNil()); }
var ret_0_sym: fun(ref KPValue, ref vec<KPResult>): KPResult = fun(_: ref KPValue, x: ref vec<KPResult>): KPResult { return x[0]; }
2020-05-12 21:30:37 -04:00
var WS = grammar.add_new_nonterminal("WS", vec(grammar.add_terminal("( | |
|(;[ -~]*
2020-06-28 19:17:09 -04:00
))+", kpNil(), ret_nil_term)), kpNil(), ret_nil_sym)
var optional_WS = grammar.add_new_nonterminal("optional_WS", vec<int>(), kpNil(), ret_nil_sym)
grammar.add_to_nonterminal(optional_WS, vec(WS), kpNil(), ret_nil_sym)
2020-06-28 19:17:09 -04:00
var atom = grammar.add_new_nonterminal("atom", vec(grammar.add_terminal("-?[0-9]+", kpNil(), fun(_: ref KPValue, input: ref str, l: int, r: int): KPResult { return KPResult::Ok(kpInt(string_to_num<int>(input.slice(l,r)))); })), kpNil(), ret_0_sym)
2020-05-12 21:30:37 -04:00
grammar.add_to_nonterminal(atom, vec(grammar.add_terminal("\"([#-[]| |[]-~]|(\\\\)|(\\n)|(\\t)|(\\\*)|(\\0)|
2020-06-28 19:17:09 -04:00
|[ -!]|(\\\\\"))*\"", kpNil(), fun(_: ref KPValue, input: ref str, l: int, r: int): KPResult { //"
var to_ret = str()
for (var i = l+1; i < r-1; i++;) {
if input[i] == '\\' {
if input[i+1] == 'n' {
to_ret += '\n'
} else if input[i+1] == '\\' || input[i+1] == '"' {
to_ret += input[i+1]
} else {
2020-06-28 19:17:09 -04:00
return KPResult::Err(kpString(str("bad string escape")))
}
// skip
i++
} else {
to_ret += input[i]
}
}
2020-06-28 19:17:09 -04:00
return KPResult::Ok(kpString(to_ret));
})), kpNil(), ret_0_sym)
grammar.add_to_nonterminal(atom, vec(grammar.add_terminal("-|(([a-z]|[A-Z]|_|\\*|/|\\?|\\+|!|=|&|<|>)([a-z]|[A-Z]|_|[0-9]|\\*|\\?|\\+|-|!|=|&|<|>)*)", kpNil(), fun(_: ref KPValue, input: ref str, l: int, r: int): KPResult {
var s = input.slice(l,r)
if s == "true" {
2020-06-28 19:17:09 -04:00
return KPResult::Ok(kpTrue());
} else if s == "false" {
2020-06-28 19:17:09 -04:00
return KPResult::Ok(kpFalse());
} else if s == "nil" {
2020-06-28 19:17:09 -04:00
return KPResult::Ok(kpNil());
} else {
2020-06-28 19:17:09 -04:00
return KPResult::Ok(kpSymbol(input.slice(l,r)));
}
2020-06-28 19:17:09 -04:00
})), kpNil(), ret_0_sym)
2020-06-28 19:17:09 -04:00
var form = grammar.add_new_nonterminal("form", vec(atom), kpNil(), ret_0_sym)
var space_forms = grammar.add_new_nonterminal("space_forms", vec<int>(), kpNil(), fun(_: ref KPValue, x: ref vec<KPResult>): KPResult {
return KPResult::Ok(kpVector(vec<KPValue>()))
2020-04-02 14:47:50 -04:00
})
2020-06-28 19:17:09 -04:00
grammar.add_to_nonterminal(space_forms, vec(form), kpNil(), fun(_: ref KPValue, x: ref vec<KPResult>): KPResult {
if is_err(x[0]) {
return x[0]
}
2020-06-28 19:17:09 -04:00
return KPResult::Ok(kpVector(vec(get_value(x[0]))))
})
2020-06-28 19:17:09 -04:00
grammar.add_to_nonterminal(space_forms, vec(form, WS, space_forms), kpNil(), fun(_: ref KPValue, x: ref vec<KPResult>): KPResult {
if is_err(x[0]) {
return x[0]
}
if is_err(x[2]) {
return x[2]
}
2020-06-28 19:17:09 -04:00
return KPResult::Ok(kpVector(vec(get_value(x[0])) + get_value(x[2]).get_vector_rc().get()))
})
2020-06-28 19:17:09 -04:00
grammar.add_to_nonterminal(form, vec(grammar.add_terminal("\\(", kpNil(), ret_nil_term),
2020-04-10 22:46:53 -04:00
optional_WS,
2020-06-28 19:17:09 -04:00
grammar.add_terminal("\\)", kpNil(), ret_nil_term)), kpNil(), fun(_: ref KPValue, x: ref vec<KPResult>): KPResult { return KPResult::Ok(kpVector(vec<KPValue>())); })
grammar.add_to_nonterminal(form, vec(grammar.add_terminal("\\(", kpNil(), ret_nil_term),
optional_WS,
space_forms,
optional_WS,
2020-06-28 19:17:09 -04:00
grammar.add_terminal("\\)", kpNil(), ret_nil_term)), kpNil(), fun(_: ref KPValue, x: ref vec<KPResult>): KPResult { return x[2]; })
2020-05-12 21:30:37 -04:00
grammar.set_start_symbol(form)
2020-06-28 19:17:09 -04:00
var env = new<KPEnv>()->construct()
2020-06-28 23:28:15 -04:00
env->set(str("vau"), make_builtin_combiner(str("vau"), fun(params: vec<KPValue>, dynamic_env: *KPEnv): KPResult {
var param_symbols = vec<str>()
if params.size != 3 {
return KPResult::Err(kpString(str("bad number of params to vau")))
}
if !params[0].is_symbol() {
return KPResult::Err(kpString(str("first param to vau is not symbol")))
}
var dynamic_env_name = params[0].get_symbol_text()
var is_variadic = false
var parameters = vec<str>()
if !params[1].is_vector() {
return KPResult::Err(kpString(str("second param to vau is not vector")))
}
var parameter_objects = params[1].get_vector_rc()
for (var i = 0; i < parameter_objects.get().size; i++;) {
if !parameter_objects.get()[i].is_symbol() {
return KPResult::Err(kpString(str("second param to vau has a not symbol member")))
}
var parameter = parameter_objects.get()[i].get_symbol_text()
if parameter == "&" {
is_variadic = true
} else {
parameters.add(parameter)
}
}
var to_ret.construct(dynamic_env, dynamic_env_name, parameters, is_variadic, params[2]) : KPCombiner
return KPResult::Ok(nmMV(KPValue_int::Combiner(to_ret)))
}));
env->set(str("eval"), make_builtin_combiner(str("eval"), fun(params: vec<KPValue>, dynamic_env: *KPEnv): KPResult {
var evaled_params = vec<KPValue>()
for (var i = 0; i < params.size; i++;) {
var ip = EVAL(dynamic_env, params[i])
if is_err(ip) {
return ip
}
evaled_params.add(get_value(ip))
}
if evaled_params.size == 1 {
return EVAL(dynamic_env, evaled_params[0])
} else if params.size == 2 {
if !evaled_params[1].is_env() {
return KPResult::Err(kpString(str("second param to eval is not an environment")))
}
return EVAL(evaled_params[1].get_env(), evaled_params[0])
}
return KPResult::Err(kpString(str("wrong number of params to eval")))
}));
2020-06-30 21:59:11 -04:00
env->set(str("set!"), make_builtin_combiner(str("set!"), fun(params: vec<KPValue>, dynamic_env: *KPEnv): KPResult {
if params.size != 2 {
return KPResult::Err(kpString(str("not 2 params to set!")))
}
if !params[0].is_symbol() {
return KPResult::Err(kpString(str("first parameter to set! is not a symbol")))
}
var data = EVAL(dynamic_env, params[1])
if is_err(data) {
return data
}
dynamic_env->set(params[0].get_symbol_text(), get_value(data))
return KPResult::Ok(kpNil())
}));
env->set(str("cond"), make_builtin_combiner(str("cond"), fun(params: vec<KPValue>, dynamic_env: *KPEnv): KPResult {
if (params.size % 2) != 0 {
return KPResult::Err(kpString(str("Need even number of params to cond")))
}
for (var i = 0; i < params.size; i+=2;) {
var ip = EVAL(dynamic_env, params[i])
if is_err(ip) {
return ip
}
if get_value(ip).is_truthy() {
return EVAL(dynamic_env, params[i+1])
}
}
return KPResult::Ok(kpNil())
}));
env->set(str("vector"), make_builtin_combiner(str("vector"), fun(params: vec<KPValue>, dynamic_env: *KPEnv): KPResult {
var evaled_params = vec<KPValue>()
for (var i = 0; i < params.size; i++;) {
var ip = EVAL(dynamic_env, params[i])
if is_err(ip) {
return ip
}
evaled_params.add(get_value(ip))
}
return KPResult::Ok(kpVector(evaled_params))
}));
env->set(str("len"), make_builtin_combiner(str("len"), fun(params: vec<KPValue>, dynamic_env: *KPEnv): KPResult {
if params.size != 1 {
return KPResult::Err(kpString(str("Need 1 param to len")))
}
var v = EVAL(dynamic_env, params[0]);
if is_err(v) { return v; }
return KPResult::Ok(kpInt(get_value(v).get_vector_rc().get().size))
}));
env->set(str("idx"), make_builtin_combiner(str("idx"), fun(params: vec<KPValue>, dynamic_env: *KPEnv): KPResult {
if params.size != 2 {
return KPResult::Err(kpString(str("Need 2 params to idx")))
}
var v = EVAL(dynamic_env, params[0]);
if is_err(v) { return v; }
var vv = get_value(v)
if !vv.is_vector() { return KPResult::Err(kpString(str("Param 1 to idx is not vector"))); }
var i = EVAL(dynamic_env, params[1]);
if is_err(i) { return i; }
var iv = get_value(i)
if !iv.is_int() { return KPResult::Err(kpString(str("Param 2 to idx is not int"))); }
return KPResult::Ok(vv.get_vector_rc().get()[iv.get_int()])
}));
env->set(str("concat"), make_builtin_combiner(str("concat"), fun(params: vec<KPValue>, dynamic_env: *KPEnv): KPResult {
var to_ret = vec<KPValue>()
for (var i = 0; i < params.size; i++;) {
2020-06-28 19:17:09 -04:00
var ip = EVAL(dynamic_env, params[i])
if is_err(ip) {
return ip
}
var v = get_value(ip)
if !v.is_vector() { return KPResult::Err(kpString(str("Param ") + i + " to concat is not vector")); }
to_ret += v.get_vector_rc().get()
}
return KPResult::Ok(kpVector(to_ret))
}));
env->set(str("+"), make_builtin_combiner(str("+"), fun(params: vec<KPValue>, dynamic_env: *KPEnv): KPResult {
var to_ret = 0
for (var i = 0; i < params.size; i++;) {
var ip = EVAL(dynamic_env, params[i])
if is_err(ip) { return ip; }
2020-06-28 19:17:09 -04:00
match (get_value(ip).internal) {
KPValue_int::Int(v) {
to_ret += v
continue
}
}
2020-06-28 23:28:15 -04:00
return KPResult::Err(kpString(str("called + with not an int: ") + pr_str(get_value(ip), false)))
}
2020-06-28 19:17:09 -04:00
return KPResult::Ok(kpInt(to_ret))
}));
env->set(str("-"), make_builtin_combiner(str("-"), fun(params: vec<KPValue>, dynamic_env: *KPEnv): KPResult {
var to_ret = 0
for (var i = 0; i < params.size; i++;) {
var ip = EVAL(dynamic_env, params[i])
if is_err(ip) { return ip; }
match (get_value(ip).internal) {
KPValue_int::Int(v) {
if i == 0 {
to_ret += v
} else {
to_ret -= v
}
continue
}
}
return KPResult::Err(kpString(str("called - with not an int: ") + pr_str(get_value(ip), false)))
}
return KPResult::Ok(kpInt(to_ret))
}));
env->set(str("="), make_builtin_combiner(str("="), fun(params: vec<KPValue>, dynamic_env: *KPEnv): KPResult {
if params.size != 2 {
return KPResult::Err(kpString(str("Need 2 params to =")))
}
var a = EVAL(dynamic_env, params[0]);
if is_err(a) { return a; }
var b = EVAL(dynamic_env, params[1]);
if is_err(b) { return b; }
return KPResult::Ok(kpBool(get_value(a).equals(get_value(b))))
}));
// self-implementation fun
println(rep(grammar, env, str("(set! quote (vau _ (x) x))")))
println(rep(grammar, env, str("(set! let1 (vau de (s v b) (eval (vector (vector vau (quote _) (vector s) b) (eval v de)) de)))")))
println(rep(grammar, env, str("(set! apply (vau de (f p) (eval (concat (vector (eval f de)) (eval p de)) de)))")))
println(rep(grammar, env, str("(set! rest (vau de (rl) (apply (vau _ (f & r) r) (eval rl de))))")))
println(rep(grammar, env, str("(set! map (vau de (f ml) (let1 f (eval f de) (let1 iml (eval ml de) (cond (= 0 (len iml)) (vector) true (concat (vector (f (idx iml 0))) (map f (rest iml))))))))")))
println(rep(grammar, env, str("(set! lambda (vau se (p b) (let1 f (eval (vector vau (quote _) p b) se) (vau de (& op) (apply f (map (vau dde (ip) (eval (eval ip dde) de)) op))))))")))
println(rep(grammar, env, str("(set! fun (vau se (n p b) (eval (vector set! n (vector lambda p b)) se)))")))
2020-06-28 19:17:09 -04:00
if argc == 3 && str(argv[1]) == "-C" {
error("-C not implemented")
} else if argc >= 2 {
var params = vec<KPValue>()
for (var i = 2; i < argc; i++;) {
params.add(kpString(str(argv[i])))
}
2020-06-28 19:17:09 -04:00
env->set(str("*ARGV*"), kpVector(params))
var eval_result_str = rep(grammar, env, str("(load-file \"") + argv[1] + "\")")
println(eval_result_str)
if eval_result_str.length() >= 11 && eval_result_str.slice(0,11) == "Exception: " {
error("aborting compile")
}
// check for compile
var main = env->get(str("main"))
if !is_err(main) {
println("Starting compile!")
2020-06-28 19:17:09 -04:00
error("Compiler not implemented")
} else {
println("No main function to compile, exiting")
}
} else {
while (true) {
var line = get_line(str("user> "), 1024)
if (line == "***EOF***")
break
2020-05-12 21:30:37 -04:00
println(rep(grammar, env, line))
}
}
}