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:
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;

11
com.c
View File

@ -612,13 +612,10 @@ 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)
case Oandand: case Ooror:
case Oeq: case Oneq:
case Olt: case Ogt:
case Ogeq: case Oleq:
cmpcom(nto, n);
break;
case Omul:

6
dat.h
View File

@ -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;

7
file.c
View File

@ -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;
}
}

3
gen.c
View File

@ -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);

12
lex.c
View File

@ -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);

5
main.c
View File

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

21
parse.c
View File

@ -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 }