Skip to content

Commit

Permalink
chuck: merge eval_ast and macroexpand into EVAL
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon authored and kanaka committed Oct 22, 2024
1 parent d1cc553 commit 1873468
Show file tree
Hide file tree
Showing 9 changed files with 615 additions and 928 deletions.
104 changes: 47 additions & 57 deletions impls/chuck/step2_eval.ck
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,10 @@ fun MalObject READ(string input)

fun MalObject EVAL(MalObject m, MalSubr env[])
{
if( m.type == "list" )
{
if( m.objects.size() == 0 )
{
return m;
}

eval_ast(m, env) @=> MalObject result;
if( result.type == "error" )
{
return result;
}

result.malObjectValues() @=> MalObject values[];
values[0]$MalSubr @=> MalSubr subr;
MalObject.slice(values, 1) @=> MalObject args[];

return subr.call(args);
}
else
{
return eval_ast(m, env);
}
}

fun MalObject eval_ast(MalObject m, MalSubr env[])
{
m.type => string type;
// Util.println("EVAL: " + Printer.pr_str(m, true));

if( type == "symbol" )
if( m.type == "symbol" )
{
m.stringValue => string symbol;
env[symbol] @=> MalSubr subr;
Expand All @@ -70,55 +44,71 @@ fun MalObject eval_ast(MalObject m, MalSubr env[])
return subr;
}
}
else if( type == "list" || type == "vector" || type == "hashmap" )
else if( m.type == "vector" )
{
m.malObjectValues() @=> MalObject values[];
MalObject results[values.size()];

if( type != "hashmap" )
for( 0 => int i; i < values.size(); i++ )
{
for( 0 => int i; i < values.size(); i++ )
EVAL(values[i], env) @=> MalObject result;
if( result.type == "error" )
{
EVAL(values[i], env) @=> MalObject result;

if( result.type == "error" )
{
return result;
}

result @=> results[i];
return result;
}
result @=> results[i];
}
else
return MalVector.create(results);
}
else if( m.type == "hashmap" )
{
m.malObjectValues() @=> MalObject values[];
MalObject results[values.size()];
for( 0 => int i; i < values.size(); i++ )
{
for( 0 => int i; i < values.size(); i++ )
if( i % 2 == 0 )
{
if( i % 2 == 0 )
{
values[i] @=> results[i];
}
else
{
EVAL(values[i], env) @=> results[i];
}
values[i] @=> results[i];
}
else
{
EVAL(values[i], env) @=> results[i];
}
}
return MalHashMap.create(results);
}
else if( m.type == "list" )
{
m.malObjectValues() @=> MalObject ast[];

if( ast.size() == 0 )
{
return m;
}

if( type == "list" )
EVAL(ast[0], env) @=> MalObject first;
if( first.type == "error" )
{
return MalList.create(results);
return first;
}
else if( type == "vector" )

MalObject args[ast.size() - 1];
for( 0 => int i; i < args.size(); i++ )
{
return MalVector.create(results);
EVAL(ast[i + 1], env) @=> MalObject result;
if( result.type == "error" )
{
return result;
}
result @=> args[i];
}
else if( type == "hashmap" )
if( first.type == "subr" )
{
return MalHashMap.create(results);
first$MalSubr @=> MalSubr subr;
return subr.call(args);
}
else
{
Util.panic("Programmer error (exhaustive match)");
Util.panic("Programmer error: cannot apply");
return null;
}
}
Expand Down
159 changes: 73 additions & 86 deletions impls/chuck/step3_env.ck
Original file line number Diff line number Diff line change
Expand Up @@ -34,126 +34,113 @@ fun MalObject EVAL(MalObject m, Env env)
Util.println("EVAL: " + Printer.pr_str(m, true));
}

if( m.type == "list" )
if( m.type == "symbol" )
{
if( m.objects.size() == 0 )
return env.get(m.stringValue);
}
else if( m.type == "vector" )
{
m.malObjectValues() @=> MalObject values[];
MalObject results[values.size()];
for( 0 => int i; i < values.size(); i++ )
{
return m;
EVAL(values[i], env) @=> MalObject result;
if( result.type == "error" )
{
return result;
}
result @=> results[i];
}

m.malObjectValues() @=> MalObject ast[];
ast[0].stringValue => string a0;

if( a0 == "def!" )
return MalVector.create(results);
}
else if( m.type == "hashmap" )
{
m.malObjectValues() @=> MalObject values[];
MalObject results[values.size()];
for( 0 => int i; i < values.size(); i++ )
{
ast[1].stringValue => string a1;

EVAL(ast[2], env) @=> MalObject value;
if( value.type == "error" )
if( i % 2 == 0 )
{
return value;
values[i] @=> results[i];
}
else
{
EVAL(values[i], env) @=> results[i];
}
}
return MalHashMap.create(results);
}
else if( m.type == "list" )
{
m.malObjectValues() @=> MalObject ast[];

env.set(a1, value);
return value;
if( ast.size() == 0 )
{
return m;
}
else if( a0 == "let*" )
else if( ast[0].type == "symbol" )
{
Env.create(env) @=> Env let_env;
ast[1].malObjectValues() @=> MalObject bindings[];
ast[0].stringValue => string a0;

for( 0 => int i; i < bindings.size(); 2 +=> i)
if( a0 == "def!" )
{
bindings[i].stringValue => string symbol;
EVAL(bindings[i+1], let_env) @=> MalObject value;
ast[1].stringValue => string a1;

EVAL(ast[2], env) @=> MalObject value;
if( value.type == "error" )
{
return value;
}

let_env.set(symbol, value);
env.set(a1, value);
return value;
}

return EVAL(ast[2], let_env);
}

eval_ast(m, env) @=> MalObject result;
if( result.type == "error" )
{
return result;
}

result.malObjectValues() @=> MalObject values[];
values[0]$MalSubr @=> MalSubr subr;
MalObject.slice(values, 1) @=> MalObject args[];

return subr.call(args);
}
else
{
eval_ast(m, env) @=> MalObject result;
return result;
}
}

fun MalObject eval_ast(MalObject m, Env env)
{
m.type => string type;

if( type == "symbol" )
{
return env.get(m.stringValue);
}
else if( type == "list" || type == "vector" || type == "hashmap" )
{
m.malObjectValues() @=> MalObject values[];
MalObject results[values.size()];

if( type != "hashmap" )
{
for( 0 => int i; i < values.size(); i++ )
else if( a0 == "let*" )
{
EVAL(values[i], env) @=> MalObject result;
Env.create(env) @=> Env let_env;
ast[1].malObjectValues() @=> MalObject bindings[];

if( result.type == "error" )
for( 0 => int i; i < bindings.size(); 2 +=> i)
{
return result;
}
bindings[i].stringValue => string symbol;
EVAL(bindings[i+1], let_env) @=> MalObject value;

result @=> results[i];
}
}
else
{
for( 0 => int i; i < values.size(); i++ )
{
if( i % 2 == 0 )
{
values[i] @=> results[i];
}
else
{
EVAL(values[i], env) @=> results[i];
if( value.type == "error" )
{
return value;
}

let_env.set(symbol, value);
}

return EVAL(ast[2], let_env);
}
}

if( type == "list" )
EVAL(ast[0], env) @=> MalObject first;
if( first.type == "error" )
{
return MalList.create(results);
return first;
}
else if( type == "vector" )

MalObject args[ast.size() - 1];
for( 0 => int i; i < args.size(); i++ )
{
return MalVector.create(results);
EVAL(ast[i + 1], env) @=> MalObject result;
if( result.type == "error" )
{
return result;
}
result @=> args[i];
}
else if( type == "hashmap" )
if( first.type == "subr" )
{
return MalHashMap.create(results);
first$MalSubr @=> MalSubr subr;
return subr.call(args);
}
else
{
Util.panic("Programmer error (exhaustive match)");
Util.panic("Programmer error: cannot apply");
return null;
}
}
Expand Down
Loading

0 comments on commit 1873468

Please sign in to comment.