add || &&

This commit is contained in:
yoyo 2024-11-03 16:28:52 +09:00
parent f4b991f953
commit e58d1ad217
8 changed files with 56 additions and 16 deletions

View File

@ -124,6 +124,9 @@ assertexpr(Node *n)
case Oarray: case Oarray:
n->ty = isvalidty(n->ty); n->ty = isvalidty(n->ty);
break; break;
case Ooror: case Oandand:
assert(l->ty == tbool);
assert(r->ty == tbool);
case Oeq: case Oneq: case Olt: case Oeq: case Oneq: case Olt:
case Oleq: case Ogt: case Ogeq: case Oleq: case Ogt: case Ogeq:
n->ty = tbool; n->ty = tbool;

13
com.c
View File

@ -612,14 +612,11 @@ ecom(Node *nto, Node *n)
case Ocall: case Ocall:
callcom(op, n, nto); callcom(op, n, nto);
break; break;
case Oeq: case Oandand: case Ooror:
case Oneq: case Oeq: case Oneq:
case Olt: case Olt: case Ogt:
case Oleq: case Ogeq: case Oleq:
case Ogt: cmpcom(nto, n);
case Ogeq:
if(nto)
cmpcom(nto, n);
break; break;
case Omul: case Omul:
case Oadd: case Oadd:

6
dat.h
View File

@ -42,6 +42,8 @@ enum NodeOp
Oname, Oname,
Ofor, Ofor,
Oif, Oif,
Oandand,
Ooror,
Oret, Oret,
Oref, Oref,
Oxref, Oxref,
@ -135,7 +137,7 @@ enum LexToken
{ {
Leof = -1, Leof = -1,
Lnone = 0, Lnone = 0,
Lfn = sizeof(char)+1, Lfn = 5000,
Llen, Llen,
Ltid, Ltid,
Lid, Lid,
@ -157,6 +159,8 @@ enum LexToken
Lneq, Lneq,
Lleq, Lleq,
Lgeq, Lgeq,
Landand,
Loror,
}; };
typedef struct Lexer Lexer; typedef struct Lexer Lexer;

7
file.c
View File

@ -31,6 +31,7 @@ getfiles(char *path, char *sufix)
struct dirent *p; struct dirent *p;
char **s; char **s;
int ll = strlen(path);
int j = 1; int j = 1;
s = new(sizeof(char*)); s = new(sizeof(char*));
d = opendir(path); d = opendir(path);
@ -38,7 +39,11 @@ getfiles(char *path, char *sufix)
if(issufix(p->d_name, sufix)){ if(issufix(p->d_name, sufix)){
s = realloc(s, (j+1)*sizeof(void*)); s = realloc(s, (j+1)*sizeof(void*));
int l = strlen(p->d_name); 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; s[j++] = nil;
} }
} }

3
gen.c
View File

@ -239,12 +239,15 @@ genop(int op, Node *s, Node *m, Node *d)
[Olt] = {[Tint]=ILTW,[Tbool]=ILTW}, [Olt] = {[Tint]=ILTW,[Tbool]=ILTW},
[Oeq] = {[Tint]=IEQW,[Tbool]=IEQW}, [Oeq] = {[Tint]=IEQW,[Tbool]=IEQW},
[Oleq] ={[Tint]=ILEQW,[Tbool]=ILEQW}, [Oleq] ={[Tint]=ILEQW,[Tbool]=ILEQW},
[Oandand] = {[Tint]=IEQW,[Tbool]=IEQW},
[Ooror] = {[Tint]=IADDW,[Tbool]=IADDW},
[Oadd] = {[Tint]=IADDW,}, [Oadd] = {[Tint]=IADDW,},
[Osub] = {[Tint]=ISUBW,}, [Osub] = {[Tint]=ISUBW,},
[Omul] = {[Tint]=IMULW,}, [Omul] = {[Tint]=IMULW,},
}; };
Inst *in = mkinst(); Inst *in = mkinst();
int iop = disoptab[op][d->ty->kind]; int iop = disoptab[op][d->ty->kind];
assert(iop != nil);
in->op = iop; in->op = iop;
if(s){ if(s){
in->s = genaddr(s); in->s = genaddr(s);

12
lex.c
View File

@ -146,6 +146,16 @@ lexing(void)
case '(': case ')': case '[': case ']': case '(': case ')': case '[': case ']':
case '}': case '{': case ',': case ';': case '}': case '{': case ',': case ';':
return (Token){.kind=c}; 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 '!': case '!':
if((c = Getc()) == '=') if((c = Getc()) == '=')
return (Token){.kind=Lneq}; return (Token){.kind=Lneq};
@ -173,7 +183,7 @@ lexing(void)
return (Token){.kind=Ldas}; return (Token){.kind=Ldas};
Ungetc(c); Ungetc(c);
return (Token){.kind=':'}; return (Token){.kind=':'};
case '+':case '-': case '*': case '&': case '+':case '-': case '*':
return (Token){.kind=c}; return (Token){.kind=c};
default: default:
assert(0); assert(0);

7
main.c
View File

@ -83,10 +83,13 @@ buildpkg(char *path, char **srcs, int n)
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
path = getpath(); if(argc == 2)
path = argv[1];
else
path = getpath();
char **srcs = getfiles(path, ".yo"); char **srcs = getfiles(path, ".yo");
if(nsrc == 0) if(nsrc == 0)
return; return 0;
lexinit(); lexinit();
typeinit(); typeinit();

21
parse.c
View File

@ -329,8 +329,7 @@ addop(void)
switch(c){ switch(c){
case '+': p = mkn(Oadd, p, mulop()); break; case '+': p = mkn(Oadd, p, mulop()); break;
case '-': p = mkn(Osub, p, mulop()); break; case '-': p = mkn(Osub, p, mulop()); break;
default: default: assert(0);
assert(0);
} }
return p; return p;
} }
@ -355,10 +354,26 @@ relop(void)
return p; 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* static Node*
expr(void) expr(void)
{ {
return relop(); return logicop();
} }
// exprlist = expr {',' expr } // exprlist = expr {',' expr }