From e8b34707b8ce58c98de75b7722fea632f8d95f84 Mon Sep 17 00:00:00 2001 From: M4GNV5 Date: Tue, 2 Feb 2016 17:12:58 +0100 Subject: [PATCH] implemented associativity and fixed small bug with prefixed ++ and -- --- cparse.js | 21 ++++++++++++++------- tests/simple.c | 20 +++++++++++--------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/cparse.js b/cparse.js index 43fb087..bd680b2 100644 --- a/cparse.js +++ b/cparse.js @@ -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", @@ -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); @@ -421,6 +429,7 @@ var cparse = (function() { if(lookahead(op)) { + console.log(op); handleOp(op); return; } @@ -463,8 +472,6 @@ var cparse = (function() return 0; } - console.dir(postfix); - function toTree() { var count = opArgCount(postfix[i]); diff --git a/tests/simple.c b/tests/simple.c index 6982e81..aa1cbfe 100644 --- a/tests/simple.c +++ b/tests/simple.c @@ -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'}; }