Skip to content

Commit

Permalink
varによる変数の宣言を実装
Browse files Browse the repository at this point in the history
  • Loading branch information
k-fog committed May 8, 2023
1 parent 52bb802 commit ef53703
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
9 changes: 9 additions & 0 deletions eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ pVal *eval(Node *node, Env *env) {
put(global_env, node->name->str, pv);
return pv;
}
case ND_VARS:
{
node = node->body->next;
while (node != NULL) {
put(global_env, node->str, pInt(0));
node = node->next;
}
return pInt(0);
}
case ND_FNCALL:
{
Env *fn_env = new_env(global_env);
Expand Down
11 changes: 10 additions & 1 deletion parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,16 @@ static Node *block() {
}

static Node *var_decl() {
return NULL;
if (!eq(read(), "var")) exit(1);
Node *head = new_node(ND_VARS);
head->body = new_node(ND_NULL);
Node *node = head->body->next = new_ident(read());
while (eq(peek(), ",")) {
read();
node = node->next = new_ident(read());
}
if (!eq(read(), ";")) exit(1);
return head;
}
static Node *const_decl() {
return NULL;
Expand Down
1 change: 1 addition & 0 deletions pl0.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typedef enum {
ND_WRITELN,// writeln
ND_ASSG, // assignment
ND_BEGIN, // begin ... end
ND_VARS, // var
ND_FNDEF, // function
ND_ARGS, // args
ND_PARAMS,// parameters
Expand Down
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ end;
return gcd(13*4, 13*9).'
assert 20 'function a(x) return x; function b(x) return 2*a(x); return b(10).'
assert "123"$'\n'"0" 'begin write 123; writeln; end.'
assert 32 'var hoge; begin hoge:=32; return hoge; end.'

echo OK!

0 comments on commit ef53703

Please sign in to comment.