Skip to content

Commit

Permalink
implemented associativity and fixed small bug with prefixed ++ and --
Browse files Browse the repository at this point in the history
  • Loading branch information
M4GNV5 committed Feb 2, 2016
1 parent c061d93 commit e8b3470
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
21 changes: 14 additions & 7 deletions cparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,27 @@ var cparse = (function()
};

const prefixedOps = {
"++": 12, //prefixed ++
"--": 12, //prefixed --
"!": 12, //logical NOT
"~": 12, //bitwise NOT
"&": 12, //adress of
"*": 12, //dereference
"+": 12, //unary +
"-": 12, //unary -
"++": 12, //prefixed ++
"--": 12, //prefixed --
"sizeof": 12
}
};

const suffixedOps = {
"++": 13, //suffixed ++
"--": 13 //suffixed --
}
};

const rightToLeftAssociativity = {
"1": true,
"2": true,
"12": true
};

const stringEscapes = {
"a": "\a",
Expand Down Expand Up @@ -283,7 +289,9 @@ var cparse = (function()
wasOp = true;

var prec = getPrecendence(op);
while(opstack[0] && getPrecendence(opstack[0]) > prec)
if(rightToLeftAssociativity[prec])
prec++;
while(opstack[0] && getPrecendence(opstack[0]) >= prec)
{
postfix.push(opstack[0]);
opstack.splice(0, 1);
Expand Down Expand Up @@ -421,6 +429,7 @@ var cparse = (function()
{
if(lookahead(op))
{
console.log(op);
handleOp(op);
return;
}
Expand Down Expand Up @@ -463,8 +472,6 @@ var cparse = (function()
return 0;
}

console.dir(postfix);

function toTree()
{
var count = opArgCount(postfix[i]);
Expand Down
20 changes: 11 additions & 9 deletions tests/simple.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
void main()
struct person
{
a = 6 * 7;
b = foo(42, 666);
c = bar();
d = {42, 666, 1337, 3112};
e = "hello";
g = foo[1];
h = foo->bar * foo.bar;
i = sizeof f;
const char *sex;
char *name;
int age;
}

const char *main()
{
int x = x > 5 ? 0 : x++;
const char msg[] = "hello";
char msg2[] = {'h', 'i'};
}

0 comments on commit e8b3470

Please sign in to comment.