Skip to content

Commit

Permalink
better structure pointer types in the ast which means we can now also…
Browse files Browse the repository at this point in the history
… support sized array definitions
  • Loading branch information
M4GNV5 committed Aug 23, 2017
1 parent 45537be commit 67afeef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
]
Expand All @@ -58,7 +60,7 @@ outputs following AST:
```
"THE BEER-WARE LICENSE":
Jakob Löw <[email protected]> wrote this code. As long as you retain this notice you
Jakob Löw <[email protected]> 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.
```
41 changes: 33 additions & 8 deletions cparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ var cparse = (function()
{
expr = {
type: "CastExpression",
typeDef: readDefinition(true),
targetType: readDefinition(true),
};
consume(")");
expr.value = parseUnary()
Expand Down Expand Up @@ -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()
};

Expand All @@ -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;
}
}
Expand Down

0 comments on commit 67afeef

Please sign in to comment.