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

@ -14,12 +14,12 @@ proc addReorder*(): NimNode =
proc addHeader*(fullpath: string) = proc addHeader*(fullpath: string) =
gCurrentHeader = ("header" & fullpath.splitFile().name.replace(re"[-.]+", "")) gCurrentHeader = ("header" & fullpath.splitFile().name.replace(re"[-.]+", ""))
gConstStr &= &" {gCurrentHeader} = \"{fullpath}\" # addHeader()\n" gConstStr &= &" {gCurrentHeader} = \"{fullpath}\" # addHeader()\n"
# #
# Preprocessor # Preprocessor
# #
proc preprocDef(node: ref Ast) = proc preprocDef(node: ref Ast) =
if node.children.len() == 2: if node.children.len() == 2:
let let
@ -30,7 +30,7 @@ proc preprocDef(node: ref Ast) =
gConsts.add(name) gConsts.add(name)
if val.getLit().nBl: if val.getLit().nBl:
# #define NAME VALUE # #define NAME VALUE
gConstStr &= &" {name.getIdentifier()}* = {val} # preprocDef()\n" gConstStr &= &" {name.getIdentifier()}* = {val} # preprocDef()\n"
# #
# Types # Types
@ -44,7 +44,7 @@ proc typeScan(node: ref Ast, sym, identifier, offset: string): string =
pname = getNodeValIf(node.children[1], identifier) pname = getNodeValIf(node.children[1], identifier)
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 pname.len() == 0: if pname.len() == 0:
return return
elif ptyp.nBl: elif ptyp.nBl:
@ -68,28 +68,28 @@ proc structSpecifier(node: ref Ast, name = "") =
gTypes.add(name) gTypes.add(name)
if name != typ: if name != typ:
# typedef struct X Y # typedef struct X Y
gTypeStr &= &" {name}* = {typ} #1 structSpecifier()\n" gTypeStr &= &" {name}* = {typ} #1 structSpecifier()\n"
else: else:
# typedef struct X X # typedef struct X X
gTypeStr &= &" {name}* {{.importc: \"{name}\", header: {gCurrentHeader}, bycopy.}} = object #2 structSpecifier()\n" gTypeStr &= &" {name}* {{.importc: \"{name}\", header: {gCurrentHeader}, bycopy.}} = object #2 structSpecifier()\n"
of "field_declaration_list": of "field_declaration_list":
# typedef struct { fields } X # typedef struct { fields } X
stmt = &" {name}* {{.importc: \"{name}\", header: {gCurrentHeader}, bycopy.}} = object #3 structSpecifier()\n" stmt = &" {name}* {{.importc: \"{name}\", header: {gCurrentHeader}, bycopy.}} = object #3 structSpecifier()\n"
for field in node.children[0].children: for field in node.children[0].children:
let ts = typeScan(field, "field_declaration", "field_identifier", " ") let ts = typeScan(field, "field_declaration", "field_identifier", " ")
if ts.len() == 0: if ts.len() == 0:
return return
stmt &= ts & "\n" stmt &= ts & "\n"
gTypes.add(name) gTypes.add(name)
gTypeStr &= stmt gTypeStr &= stmt
elif name.len() == 0 and node.children.len() == 2 and node.children[1].sym == "field_declaration_list": elif name.len() == 0 and node.children.len() == 2 and node.children[1].sym == "field_declaration_list":
let ename = getNodeValIf(node.children[0], "type_identifier") let ename = getNodeValIf(node.children[0], "type_identifier")
if ename.nBl and ename notin gTypes: if ename.nBl and ename notin gTypes:
# struct X { fields } # struct X { fields }
stmt &= &" {ename}* {{.importc: \"struct {ename}\", header: {gCurrentHeader}, bycopy.}} = object #4 structSpecifier()\n" stmt &= &" {ename}* {{.importc: \"struct {ename}\", header: {gCurrentHeader}, bycopy.}} = object #4 structSpecifier()\n"
for field in node.children[1].children: for field in node.children[1].children:
let ts = typeScan(field, "field_declaration", "field_identifier", " ") let ts = typeScan(field, "field_declaration", "field_identifier", " ")
@ -105,18 +105,18 @@ proc enumSpecifier(node: ref Ast, name = "") =
ename: string ename: string
elid: int elid: int
stmt: string stmt: string
if node.children.len() == 1 and node.children[0].sym == "enumerator_list": if node.children.len() == 1 and node.children[0].sym == "enumerator_list":
# typedef enum { fields } X # typedef enum { fields } X
ename = name ename = name
elid = 0 elid = 0
stmt = &" {name}* = enum #1 enumSpecifier()\n" stmt = &" {name}* = enum #1 enumSpecifier()\n"
elif name.len() == 0 and node.children.len() == 2 and node.children[1].sym == "enumerator_list": elif name.len() == 0 and node.children.len() == 2 and node.children[1].sym == "enumerator_list":
ename = getNodeValIf(node.children[0], "type_identifier") ename = getNodeValIf(node.children[0], "type_identifier")
elid = 1 elid = 1
if ename.nBl: if ename.nBl:
# enum X { fields } # enum X { fields }
stmt = &" {ename}* = enum #2 enumSpecifier()\n" stmt = &" {ename}* = enum #2 enumSpecifier()\n"
else: else:
return return
@ -130,27 +130,31 @@ proc enumSpecifier(node: ref Ast, name = "") =
stmt &= &" {fname} = {num}\n" stmt &= &" {fname} = {num}\n"
else: else:
return return
if ename notin gTypes: if ename notin gTypes:
gTypes.add(name) gTypes.add(name)
gTypeStr &= stmt gTypeStr &= stmt
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
gTypes.add(name) gTypes.add(name)
gTypeStr &= &" {name}* = {ptyp.getType()} #1 typeDefinition()\n" gTypeStr &= &" {name}* = {ptyp.getType()} #1 typeDefinition()\n"
elif ttyp.nBl: elif ttyp.nBl:
# typedef X Y # typedef X Y
gTypes.add(name) gTypes.add(name)
gTypeStr &= &" {name}* = {ttyp} #2 typeDefinition()\n" gTypeStr &= &" {name}* = {ttyp} #2 typeDefinition()\n"
else: else:
case node.children[0].sym: case node.children[0].sym:
of "struct_specifier": of "struct_specifier":
@ -162,11 +166,11 @@ proc functionDeclarator(node: ref Ast, typ: string) =
if node.children.len() == 2: if node.children.len() == 2:
let let
name = getNodeValIf(node.children[0], "identifier") name = getNodeValIf(node.children[0], "identifier")
if name.nBl and name notin gProcs and node.children[1].sym == "parameter_list": if name.nBl and name notin gProcs and node.children[1].sym == "parameter_list":
# typ function(typ param1, ...) # typ function(typ param1, ...)
var stmt = &"# functionDeclarator()\nproc {name}*(" var stmt = &"# functionDeclarator()\nproc {name}*("
for i in 0 .. node.children[1].children.len()-1: for i in 0 .. node.children[1].children.len()-1:
let ts = typeScan(node.children[1].children[i], "parameter_declaration", "identifier", "") let ts = typeScan(node.children[1].children[i], "parameter_declaration", "identifier", "")
if ts.len() == 0: if ts.len() == 0:
@ -174,17 +178,17 @@ proc functionDeclarator(node: ref Ast, typ: string) =
stmt &= ts stmt &= ts
if i != node.children[1].children.len()-1: if i != node.children[1].children.len()-1:
stmt &= ", " stmt &= ", "
if typ != "void": if typ != "void":
stmt &= &"): {typ.getType()} " stmt &= &"): {typ.getType()} "
else: else:
stmt &= ") " stmt &= ") "
stmt &= &"{{.importc: \"{name}\", header: {gCurrentHeader}.}}\n" stmt &= &"{{.importc: \"{name}\", header: {gCurrentHeader}.}}\n"
gProcs.add(name) gProcs.add(name)
gProcStr &= stmt gProcStr &= stmt
proc declaration*(node: ref Ast) = proc declaration*(node: ref Ast) =
if node.children.len() == 2 and node.children[1].sym == "function_declarator": if node.children.len() == 2 and node.children[1].sym == "function_declarator":
let let
@ -199,7 +203,7 @@ proc declaration*(node: ref Ast) =
let styp = getNodeValIf(node.children[0].children[0], "type_identifier") let styp = getNodeValIf(node.children[0].children[0], "type_identifier")
if styp.nBl: if styp.nBl:
functionDeclarator(node.children[1], styp) functionDeclarator(node.children[1], styp)
proc genNimAst*(node: ref Ast) = proc genNimAst*(node: ref Ast) =
case node.sym: case node.sym:
of "ERROR": of "ERROR":
@ -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:
echo "" if gPretty:
echo ""
nextnode = node.tsNodeNamedChild(0) nextnode = node.tsNodeNamedChild(0)
depth += 1 depth += 1
else: else:
echo ")" if gPretty:
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
echo spaces(depth) & ")" if gPretty:
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)