shithub: mc

Download patch

ref: 45c4886b0fa21533658807dba33a030b37337c8d
parent: 76d42756c93a7f8e1519268b8eb693ba36db4652
author: Ori Bernstein <[email protected]>
date: Sat Nov 5 08:22:19 EDT 2011

More work towards fully parsing.

--- a/parse/gram.y
+++ b/parse/gram.y
@@ -115,15 +115,15 @@
 
 %type <node> exprln retexpr expr atomicexpr literal asnexpr lorexpr landexpr borexpr
 %type <node> bandexpr cmpexpr addexpr mulexpr shiftexpr prefixexpr postfixexpr
-%type <node> funclit arraylit arglist name
+%type <node> funclit arraylit name
 %type <node> decl declvariants declbody declcore structelt enumelt unionelt
 
-%type <nodelist> argdefs structbody enumbody unionbody
+%type <nodelist> arglist argdefs structbody enumbody unionbody
 
 %union {
     struct {
-        Node **nodes;
-        size_t nnodes;
+        Node **nl;
+        size_t nn;
     } nodelist;
     struct {
         Type **types;
@@ -219,21 +219,21 @@
 functype: TOparen funcsig TCparen {$$ = $2;}
         ;
 
-funcsig : argdefs {$$ = mktyfunc(line, $1.nodes, $1.nnodes, mktyvar(line));}
-        | argdefs TRet type {$$ = mktyfunc(line, $1.nodes, $1.nnodes, $3);}
+funcsig : argdefs {$$ = mktyfunc(line, $1.nl, $1.nn, mktyvar(line));}
+        | argdefs TRet type {$$ = mktyfunc(line, $1.nl, $1.nn, $3);}
         ;
 
-argdefs : declcore {$$.nodes = NULL; $$.nnodes = 0; nlappend(&$$.nodes, &$$.nnodes, $1);}
-        | argdefs TComma declcore {nlappend(&$$.nodes, &$$.nnodes, $3);}
+argdefs : declcore {$$.nl = NULL; $$.nn = 0; nlappend(&$$.nl, &$$.nn, $1);}
+        | argdefs TComma declcore {nlappend(&$$.nl, &$$.nn, $3);}
         ;
 
 structdef
-        : TStruct structbody TEndblk {$$ = mktystruct($1->line, $2.nodes, $2.nnodes);}
+        : TStruct structbody TEndblk {$$ = mktystruct($1->line, $2.nl, $2.nn);}
         ;
 
 structbody
-        : structelt {$$.nnodes = 0; nlappend(&$$.nodes, &$$.nnodes, $1);}
-        | structbody structelt {if ($2) {nlappend(&$$.nodes, &$$.nnodes, $2);}}
+        : structelt {$$.nl = NULL; $$.nn = 0; nlappend(&$$.nl, &$$.nn, $1);}
+        | structbody structelt {if ($2) {nlappend(&$$.nl, &$$.nn, $2);}}
         ;
 
 structelt
@@ -242,12 +242,12 @@
         ;
 
 uniondef
-        : TUnion unionbody TEndblk {$$ = mktyunion(line, $2.nodes, $2.nnodes);}
+        : TUnion unionbody TEndblk {$$ = mktyunion(line, $2.nl, $2.nn);}
         ;
 
 unionbody
-        : unionelt {$$.nnodes = 0; nlappend(&$$.nodes, &$$.nnodes, $1);}
-        | unionbody unionelt {if ($2) {nlappend(&$$.nodes, &$$.nnodes, $2);}}
+        : unionelt {$$.nl = NULL; $$.nn = 0; nlappend(&$$.nl, &$$.nn, $1);}
+        | unionbody unionelt {if ($2) {nlappend(&$$.nl, &$$.nn, $2);}}
         ;
 
 unionelt
@@ -255,11 +255,11 @@
         | visdef TEndln {$$ = NULL;}
         ;
 
-enumdef : TEnum enumbody TEndblk {$$ = mktyenum($1->line, $2.nodes, $2.nnodes);}
+enumdef : TEnum enumbody TEndblk {$$ = mktyenum($1->line, $2.nl, $2.nn);}
         ;
 
-enumbody: enumelt {$$.nnodes = 0; nlappend(&$$.nodes, &$$.nnodes, $1);}
-        | enumbody enumelt {if ($2) {nlappend(&$$.nodes, &$$.nnodes, $2);}}
+enumbody: enumelt {$$.nl = NULL; $$.nn = 0; if ($1) nlappend(&$$.nl, &$$.nn, $1);}
+        | enumbody enumelt {if ($2) {nlappend(&$$.nl, &$$.nn, $2);}}
         ;
 
 enumelt : TIdent TEndln {$$ = NULL; die("enumelt impl");}
@@ -357,12 +357,13 @@
         | postfixexpr TOsqbrac expr TComma expr TCsqbrac {
                 $$ = mkexpr($1->line, Oslice, $1, $3, $5, NULL);
             }
-        | postfixexpr TOparen arglist TCparen {$$ = mkexpr($1->line, Ocall, $1, $3);}
+        | postfixexpr TOparen arglist TCparen {$$ = mkcall($1->line, $1, $3.nl, $3.nn);}
         | atomicexpr
         ;
 
-arglist : asnexpr
-        | arglist TComma asnexpr
+arglist : asnexpr {$$.nl = NULL; $$.nn = 0; nlappend(&$$.nl, &$$.nn, $1);}
+        | arglist TComma asnexpr {nlappend(&$$.nl, &$$.nn, $3);}
+        | /* empty */ {$$.nl = NULL; $$.nn = 0;}
         ;
 
 atomicexpr
--- a/parse/node.c
+++ b/parse/node.c
@@ -58,6 +58,17 @@
     return n;
 }
 
+Node *mkcall(int line, Node *fn, Node **args, size_t nargs) 
+{
+    Node *n;
+    int i;
+
+    n = mkexpr(line, Ocall, fn, NULL);
+    for (i = 0; i < nargs; i++)
+        nlappend(&n->expr.args, &n->expr.nargs, args[i]);
+    return n;
+}
+
 Node *mkif(int line, Node *cond, Node *iftrue, Node *iffalse)
 {
     Node *n;
@@ -141,8 +152,11 @@
 
 Node *mkdecl(int line, Sym *sym)
 {
-    die("Fixme: mkdecl");
-    return NULL;
+    Node *n;
+
+    n = mknode(line, Ndecl);
+    n->decl.sym = sym;
+    return n;
 }
 
 void setns(Node *n, char *name)
@@ -169,13 +183,18 @@
 
 Sym *mksym(int line, Node *name, Type *ty)
 {
-    die("Fixme: mksym");
-    return NULL;
+    Sym *sym;
+
+    sym = zalloc(sizeof(Sym));
+    sym->name = name;
+    sym->type = ty;
+    sym->line = line;
+    return sym;
 }
 
 void nlappend(Node ***nl, size_t *len, Node *n)
 {
-    *nl = xrealloc(nl, (*len + 1)*sizeof(Node*));
+    *nl = xrealloc(*nl, (*len + 1)*sizeof(Node*));
     (*nl)[*len] = n;
     (*len)++;
 }
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -44,6 +44,7 @@
 };
 
 struct Sym {
+    int   line;
     Node *name;
     Type *type;
 };
@@ -178,6 +179,7 @@
 Node *mkfile(char *name);
 Node *mkuse(int line, char *use, int islocal);
 Node *mkexpr(int line, Op op, ...); /* NULL terminated */
+Node *mkcall(int line, Node *fn, Node **args, size_t nargs);
 Node *mklit(int line, Littype lt, void *val);
 Node *mkif(int line, Node *cond, Node *iftrue, Node *iffalse);
 Node *mkloop(int line, Node *init, Node *cond, Node *incr, Node *body);
--- a/parse/tok.c
+++ b/parse/tok.c
@@ -13,7 +13,7 @@
 
 #include "parse.h"
 
-#include "gram.h"
+#include "y.tab.h"
 
 char *filename;
 int line;
@@ -226,7 +226,7 @@
         else if (c == '\n')
             fatal(line, "Newlines not allowed in char lit");
     };
-    t = mktok(TChrlit);
+    t = mktok(TStrlit);
     t->str = strndup(&fbuf[sstart], fidx - sstart);
     return t;
 }
@@ -461,6 +461,8 @@
     fd = open(file, O_RDONLY);
     if (fd == -1)
         err(errno, "Unable to open file %s", file);
+
+    nread = 0;
     fbuf = malloc(4096);
     while (1) {
         n = read(fd, fbuf, 4096);