Initial pointer support, ugly toast

This commit is contained in:
Ganesh Viswanathan 2018-11-21 14:14:18 -06:00
commit 01c295a41c
4 changed files with 51 additions and 37 deletions

View file

@ -137,11 +137,15 @@ proc enumSpecifier(node: ref Ast, name = "") =
proc typeDefinition(node: ref Ast) = proc typeDefinition(node: ref Ast) =
if node.children.len() == 2: if node.children.len() == 2:
let var
name = getNodeValIf(node.children[1], "type_identifier") name = getNodeValIf(node.children[1], "type_identifier")
pname = getNodeValIf(node.children[1], "pointer_declarator")
ptyp = getNodeValIf(node.children[0], "primitive_type") ptyp = getNodeValIf(node.children[0], "primitive_type")
ttyp = getNodeValIf(node.children[0], "type_identifier") ttyp = getNodeValIf(node.children[0], "type_identifier")
if name.len() == 0 and node.children[1].sym == "pointer_declarator" and node.children[1].children.len() == 1:
name = getNodeValIf(node.children[1].children[0], "type_identifier")
if name.nBl and name notin gTypes: if name.nBl and name notin gTypes:
if ptyp.nBl: if ptyp.nBl:
# typedef int X # typedef int X
@ -217,8 +221,6 @@ proc genNimAst*(node: ref Ast) =
of "enum_specifier": of "enum_specifier":
if node.parent.sym notin ["type_definition", "declaration"]: if node.parent.sym notin ["type_definition", "declaration"]:
enumSpecifier(node) enumSpecifier(node)
of "":
return
for child in node.children: for child in node.children:
genNimAst(child) genNimAst(child)

View file

@ -8,7 +8,7 @@ proc getIdentifier*(str: string): string =
result = str.strip(chars={'_'}) result = str.strip(chars={'_'})
proc getType*(str: string): string = proc getType*(str: string): string =
result = str.strip(chars={'_'}).replace(re"([u]?int[\d]+)_t", "$1") result = str.strip(chars={'_'}).replace(re"([u]?int[\d]+)_t", "$1").replace(re"^void$", "object")
proc getLit*(str: string): string = proc getLit*(str: string): string =
if str.contains(re"^[\-]?[\d]+$") or if str.contains(re"^[\-]?[\d]+$") or

View file

@ -13,16 +13,14 @@ proc tokenize(fullpath: string) =
gTokens = @[] gTokens = @[]
idx = 0 idx = 0
for i in staticExec("toast " & fullpath): for i in staticExec("toast -u " & fullpath):
case i: case i:
of ' ', '\n', '\r', ')': of ' ', '\n', '\r', '(', ')':
if collect.nBl: if collect.nBl:
gTokens.add(collect) gTokens.add(collect)
collect = "" collect = ""
if i == ')': if i in ['(', ')']:
gTokens.add(")") gTokens.add($i)
of '(':
gTokens.add("(")
else: else:
collect &= $i collect &= $i

View file

@ -8,6 +8,9 @@ const HELP = """
> toast header.h > toast header.h
""" """
var
gPretty = true
proc printLisp(root: TSNode, data: var string) = proc printLisp(root: TSNode, data: var string) =
var var
node = root node = root
@ -16,21 +19,30 @@ proc printLisp(root: TSNode, data: var string) =
while true: while true:
if not node.tsNodeIsNull(): if not node.tsNodeIsNull():
stdout.write spaces(depth) & "(" & $node.tsNodeType() & " " & $node.tsNodeStartByte() & " " & $node.tsNodeEndByte() if gPretty:
stdout.write spaces(depth)
stdout.write "(" & $node.tsNodeType() & " " & $node.tsNodeStartByte() & " " & $node.tsNodeEndByte()
if node.tsNodeNamedChildCount() != 0: if node.tsNodeNamedChildCount() != 0:
if gPretty:
echo "" echo ""
nextnode = node.tsNodeNamedChild(0) nextnode = node.tsNodeNamedChild(0)
depth += 1 depth += 1
else: else:
if gPretty:
echo ")" echo ")"
else:
stdout.write ")"
nextnode = node.tsNodeNextNamedSibling() nextnode = node.tsNodeNextNamedSibling()
if nextnode.tsNodeIsNull(): if nextnode.tsNodeIsNull():
while true: while true:
node = node.tsNodeParent() node = node.tsNodeParent()
depth -= 1 depth -= 1
if gPretty:
echo spaces(depth) & ")" echo spaces(depth) & ")"
else:
stdout.write ")"
if node == root: if node == root:
break break
if not node.tsNodeNextNamedSibling().tsNodeIsNull(): if not node.tsNodeNextNamedSibling().tsNodeIsNull():
@ -93,6 +105,8 @@ proc parseCli() =
if param in ["-h", "--help", "-?", "/?", "/h"]: if param in ["-h", "--help", "-?", "/?", "/h"]:
echo HELP echo HELP
quit() quit()
elif param == "-u":
gPretty = false
else: else:
process(param) process(param)