From a9e3c42ac5696c56adaffbb4007e9d969563337b Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Wed, 20 May 2020 18:28:31 -0700 Subject: feat: prototype of AST --- sys/cmd/cc/cc.h | 245 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 183 insertions(+), 62 deletions(-) (limited to 'sys/cmd/cc/cc.h') diff --git a/sys/cmd/cc/cc.h b/sys/cmd/cc/cc.h index b45c5fa..144eb4a 100644 --- a/sys/cmd/cc/cc.h +++ b/sys/cmd/cc/cc.h @@ -16,7 +16,8 @@ typedef struct Lexer Lexer; typedef struct Sym Sym; typedef struct Type Type; -typedef struct Spec Spec; +typedef struct Ptr Ptr; +typedef struct Name Name; typedef struct Decl Decl; typedef struct Stmt Stmt; typedef struct Expr Expr; @@ -64,7 +65,8 @@ typedef struct Compiler Compiler; KEYWORD(Kgoto,"goto") \ KEYWORD(Kreturn,"return") \ KEYWORD(Ksizeof,"sizeof") \ - KEYWORD(Kalignof,"alignof") + KEYWORD(Kalignof,"alignof") \ + KEYWORD(Kalignas,"alignas") #define KEYWORD(a, b) a, enum { KEYWORDS NUM_KEYWORDS }; @@ -250,6 +252,10 @@ struct Lexer SymTab sym; byte *b; byte buf[2*1024]; + + /* predefined dynamic macros */ + uintptr macfile; + uintptr macline; }; /* lex.c functions */ @@ -270,96 +276,206 @@ void puttok(Token); // parsing & type resolution // tokens -> ast -/* statements */ +struct Node +{ + Range pos; + uint32 kind; +}; + +/* ast types */ enum { - Sbad, - Slabel, + Nbad, + /* labels */ + Slabel, Scase, Sblock, Sexpr, Sselect, - Sloop, - Sjump, + /* loops */ + Sfor, Swhile, Sdo, + /* jumps */ + Sgoto, Scontin, Sbreak, Sreturn, + /* forks */ + Sif, Sswitch, + + + /* assignments */ + Xasn, Xmulasn, Xdivasn, Xmodasn, Xsubasn, Xaddasn, + Xlsftasn, Xrsftasn, Xandasn, Xxorasn, Xorasn, + /* conditional */ + Xternary, + /* unary prefix ops */ + Xref, Xstar, Xplus, Xminus, Xneg, Xnot, Xsizeof, Xalnof, Xpreinc, Xpredec, + Xcast, + /* unary postfix ops */ + Xpostinc, Xpostdec, Xindex, Xcall, Xsel, Xcmpndlit, + /* binary ops */ + Xoror, Xandand, Xor, Xxor, Xand, Xeql, Xgt, Xlt, Xgteq, Xlteq, Xlsft, Xrsft, + Xadd, Xsub, Xmul, Xdiv, Xmod, + /* primary */ + Xparen, Xident, Xlit, + /* lists */ + Xcomma, + + Dfunc, + Dvar, }; +/* statements */ struct Stmt { - union - { - + struct Node; + union { + struct { + union { + string ident; + Expr *x; + }; + Stmt *stmt; + } lbl; + struct { + long n; + struct Node **item; + } blk; + Expr *x; + struct { + union { + Expr *x; + Decl *d; + } init; + Expr *cond; + Expr *step; + Stmt *body; + } loop; + union{ + string lbl; + Expr *x; + } jmp; + struct { + Expr *cond; + Stmt *body; + Stmt *orelse; + } sel; }; }; /* expressions */ -enum +struct Expr { - Xbad, - Xparen, - Xident, - Xlit, - Xassign, - Xcomma, - Xternary, - Xbinary, - Xprefix, - Xunary, - Xpostfix, - Xcast, + struct Node; + union { + struct { + Expr *l; + Expr *r; + } asn; + struct { + Expr *c; + Expr *t; + Expr *e; + } cond; + union { + Expr *pre; + Expr *post; + } unary; + struct { + uint64 to; + Expr *x; + } cast; + struct { + Expr *l; + Expr *r; + } binary; + struct { + Expr *x[2]; + } comma; + }; }; -struct Expr +/* declarations */ + +struct Ptr { - union - { + uint64 kind; + Ptr *link; +}; +struct Name +{ + struct Node; + union { + string ident; + struct Dtor *paren; + struct { + uint32 kind; + Name *base; + union { + Expr *index; + /* TODO: + * func params + * variadic arrays + * compound set notation + */ + }; + } suffix; }; }; -/* declarations */ +struct Dtor +{ + Range pos; + Ptr ptr; + Name name; +}; // specifiers enum { - Mauto = iota(0), // Corresponds to auto int + /* memory */ + Mauto = iota(0), Mtype = iota(1), Mstatic = iota(2), Mreg = iota(3), - - Qconst = iota(4), - Qrestr = iota(5), - Qvoltl = iota(6), - Finlne = iota(7), - - Tlong = iota(8), - Tvlong = iota(9), - Tsign = iota(10), - Tunsign = iota(11), - - Tvoid = iota(12), - Tchar = iota(13), - Tshort = iota(14), - Tfloat = iota(15), - Tdouble = iota(16), - Tcmplx = iota(17), - Timag = iota(18), - - Taggr = iota(19), - Tenum = iota(20), - Tname = iota(21), -}; - -enum -{ - Dnil, - Dbad, - Dfunc, - Dvar, + Mtls = iota(4), + + /* qualifiers */ + Qconst = iota(5), + Qrestr = iota(6), + Qvoltl = iota(7), + Qatom = iota(8), + + Finlne = iota(9), + Fnoret = iota(10), + + /* types */ + Tlong = iota(12), + Tvlong = iota(13), + Tsign = iota(14), + Tunsign = iota(15), + + Tvoid = iota(16), + Tchar = iota(17), + Tshort = iota(18), + Tfloat = iota(19), + Tdouble = iota(20), + Tcmplx = iota(21), + Timag = iota(22), + + Tatom = iota(23), + + Taggr = iota(24), + Tenum = iota(25), + Tname = iota(26), + + /* alignment */ + Alnas = iota(27), }; struct Decl { - Pos pos; - uint kind; + struct Node; + union { + + }; }; // ----------------------------------------------------------------------- @@ -426,9 +542,14 @@ struct Compiler string *path; } omit; - string outfile; - Lexer lxr; + + string outfile; + struct { + int cap; + int len; + Decl *decls; + } ast; }; extern Compiler C; -- cgit v1.2.1