From e58d1ad217d795f91f7cbb8f7820dfbf31276a30 Mon Sep 17 00:00:00 2001 From: yoyo Date: Sun, 3 Nov 2024 16:28:52 +0900 Subject: [PATCH] add || && --- assert.c | 3 +++ com.c | 13 +++++-------- dat.h | 6 +++++- file.c | 7 ++++++- gen.c | 3 +++ lex.c | 12 +++++++++++- main.c | 7 +++++-- parse.c | 21 ++++++++++++++++++--- 8 files changed, 56 insertions(+), 16 deletions(-) diff --git a/assert.c b/assert.c index 780164c..fd3fb17 100644 --- a/assert.c +++ b/assert.c @@ -124,6 +124,9 @@ assertexpr(Node *n) case Oarray: n->ty = isvalidty(n->ty); break; + case Ooror: case Oandand: + assert(l->ty == tbool); + assert(r->ty == tbool); case Oeq: case Oneq: case Olt: case Oleq: case Ogt: case Ogeq: n->ty = tbool; diff --git a/com.c b/com.c index 0557a2a..e329c1e 100644 --- a/com.c +++ b/com.c @@ -612,14 +612,11 @@ ecom(Node *nto, Node *n) case Ocall: callcom(op, n, nto); break; - case Oeq: - case Oneq: - case Olt: - case Oleq: - case Ogt: - case Ogeq: - if(nto) - cmpcom(nto, n); + case Oandand: case Ooror: + case Oeq: case Oneq: + case Olt: case Ogt: + case Ogeq: case Oleq: + cmpcom(nto, n); break; case Omul: case Oadd: diff --git a/dat.h b/dat.h index bf0ca6f..07c938c 100644 --- a/dat.h +++ b/dat.h @@ -42,6 +42,8 @@ enum NodeOp Oname, Ofor, Oif, + Oandand, + Ooror, Oret, Oref, Oxref, @@ -135,7 +137,7 @@ enum LexToken { Leof = -1, Lnone = 0, - Lfn = sizeof(char)+1, + Lfn = 5000, Llen, Ltid, Lid, @@ -157,6 +159,8 @@ enum LexToken Lneq, Lleq, Lgeq, + Landand, + Loror, }; typedef struct Lexer Lexer; diff --git a/file.c b/file.c index 07be08a..e8660fd 100644 --- a/file.c +++ b/file.c @@ -31,6 +31,7 @@ getfiles(char *path, char *sufix) struct dirent *p; char **s; + int ll = strlen(path); int j = 1; s = new(sizeof(char*)); d = opendir(path); @@ -38,7 +39,11 @@ getfiles(char *path, char *sufix) if(issufix(p->d_name, sufix)){ s = realloc(s, (j+1)*sizeof(void*)); int l = strlen(p->d_name); - s[j-1] = memcpy(new(l+1), p->d_name, l+1); + char *n = new(l+ll+2); + memcpy(n, path, ll); + n[ll] = '/'; + memcpy(n+ll, p->d_name, l+1); + s[j-1] = n; s[j++] = nil; } } diff --git a/gen.c b/gen.c index 02e964f..67311c5 100644 --- a/gen.c +++ b/gen.c @@ -239,12 +239,15 @@ genop(int op, Node *s, Node *m, Node *d) [Olt] = {[Tint]=ILTW,[Tbool]=ILTW}, [Oeq] = {[Tint]=IEQW,[Tbool]=IEQW}, [Oleq] ={[Tint]=ILEQW,[Tbool]=ILEQW}, + [Oandand] = {[Tint]=IEQW,[Tbool]=IEQW}, + [Ooror] = {[Tint]=IADDW,[Tbool]=IADDW}, [Oadd] = {[Tint]=IADDW,}, [Osub] = {[Tint]=ISUBW,}, [Omul] = {[Tint]=IMULW,}, }; Inst *in = mkinst(); int iop = disoptab[op][d->ty->kind]; + assert(iop != nil); in->op = iop; if(s){ in->s = genaddr(s); diff --git a/lex.c b/lex.c index 6e7138b..9b181c8 100644 --- a/lex.c +++ b/lex.c @@ -146,6 +146,16 @@ lexing(void) case '(': case ')': case '[': case ']': case '}': case '{': case ',': case ';': return (Token){.kind=c}; + case '|': + if((c = Getc()) == '|') + return (Token){.kind=Loror}; + Ungetc(c); + return (Token){.kind='|'}; + case '&': + if((c = Getc()) == '&') + return (Token){.kind=Landand}; + Ungetc(c); + return (Token){.kind='&'}; case '!': if((c = Getc()) == '=') return (Token){.kind=Lneq}; @@ -173,7 +183,7 @@ lexing(void) return (Token){.kind=Ldas}; Ungetc(c); return (Token){.kind=':'}; - case '+':case '-': case '*': case '&': + case '+':case '-': case '*': return (Token){.kind=c}; default: assert(0); diff --git a/main.c b/main.c index d2cb98b..2bf495a 100644 --- a/main.c +++ b/main.c @@ -83,10 +83,13 @@ buildpkg(char *path, char **srcs, int n) int main(int argc, char **argv) { - path = getpath(); + if(argc == 2) + path = argv[1]; + else + path = getpath(); char **srcs = getfiles(path, ".yo"); if(nsrc == 0) - return; + return 0; lexinit(); typeinit(); diff --git a/parse.c b/parse.c index 7554509..41a221e 100644 --- a/parse.c +++ b/parse.c @@ -329,8 +329,7 @@ addop(void) switch(c){ case '+': p = mkn(Oadd, p, mulop()); break; case '-': p = mkn(Osub, p, mulop()); break; - default: - assert(0); + default: assert(0); } return p; } @@ -355,10 +354,26 @@ relop(void) return p; } +static Node* +logicop(void) +{ + int c, op; + Node *p = relop(); + while((c = try((int[]){Loror,Landand,0}).kind)){ + switch(c){ + case Loror: op = Ooror; break; + case Landand: op = Oandand; break; + default: assert(0); + } + p = mkn(op, p, relop()); + } + return p; +} + static Node* expr(void) { - return relop(); + return logicop(); } // exprlist = expr {',' expr }