diff --git a/README.md b/README.md index d2c0fa1..988c9e3 100644 --- a/README.md +++ b/README.md @@ -34,21 +34,23 @@ outputs following AST: [ { "type": "GlobalVariableDeclaration", - "modifier": [], - "pointer": 0, + "defType": { + "type": "Type", + "modifier": [], + "name": "int" + }, "name": "answer", - "valueType": "int", "value": { "type": "BinaryExpression", "operator": "*", - "right": { + "left": { "type": "Literal", - "value": 7 + "value": 6, }, - "left": { + "right": { "type": "Literal", - "value": 6 - } + "value": 7, + }, } } ] @@ -58,7 +60,7 @@ outputs following AST: ``` "THE BEER-WARE LICENSE": -Jakob Löw wrote this code. As long as you retain this notice you +Jakob Löw wrote this code. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return. ``` diff --git a/cparse.js b/cparse.js index b57e8e0..e287c5e 100644 --- a/cparse.js +++ b/cparse.js @@ -401,7 +401,7 @@ var cparse = (function() { expr = { type: "CastExpression", - typeDef: readDefinition(true), + targetType: readDefinition(true), }; consume(")"); expr.value = parseUnary() @@ -544,10 +544,11 @@ var cparse = (function() } function readDefinition(nameless) { + var name; + var pos = getPos(); var def = { - type: "Definition", + type: "Type", modifier: [], - pointer: 0, pos: getPos() }; @@ -568,21 +569,45 @@ var cparse = (function() { if(lookahead(typeNames[i])) { - def.typeName = typeNames[i]; + def.name = typeNames[i]; while(lookahead("*")) { - def.pointer++; + //TODO allow 'const' in between + def = { + type: "PointerType", + target: def, + pos: getPos() + }; } if(!nameless) - def.name = readIdentifier(); + name = readIdentifier(); - while(lookahead("[]")) //TODO [length] + while(lookahead("[")) { - def.pointer++; + def = { + type: "PointerType", + target: def, + pos: getPos() + }; + + if(!lookahead("]")) + { + def.length = parseExpression(); + consume("]"); + } } + if(name) + { + def = { + type: "Definition", + defType: def, + name: name, + pos: pos + }; + } return def; } }