Initial pointer support, ugly toast
This commit is contained in:
parent
047e33868f
commit
01c295a41c
4 changed files with 51 additions and 37 deletions
|
|
@ -14,7 +14,7 @@ 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
|
||||||
|
|
@ -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
|
||||||
|
|
@ -68,14 +68,14 @@ 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", " ")
|
||||||
|
|
@ -89,7 +89,7 @@ proc structSpecifier(node: ref Ast, name = "") =
|
||||||
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", " ")
|
||||||
|
|
@ -110,13 +110,13 @@ proc enumSpecifier(node: ref Ast, name = "") =
|
||||||
# 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
|
||||||
|
|
||||||
|
|
@ -137,20 +137,24 @@ 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
|
||||||
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":
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
||||||
22
toast.nim
22
toast.nim
|
|
@ -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)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue