Remove enum converter - #72

This commit is contained in:
Ganesh Viswanathan 2019-01-29 23:30:01 -06:00
commit 127abe1383
4 changed files with 73 additions and 27 deletions

View file

@ -99,7 +99,7 @@ proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =
if searchAstForNode(ast, node, nimState): if searchAstForNode(ast, node, nimState):
ast.tonim(ast, node, nimState) ast.tonim(ast, node, nimState)
if gStateRT.debug: if gStateRT.debug:
nimState.debugStr &= "\n\n# " & nimState.data.join("\n# ") nimState.debugStr &= "\n# " & nimState.data.join("\n# ")
break break
nimState.data = @[] nimState.data = @[]
else: else:
@ -143,7 +143,7 @@ proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) =
nimState.identifiers = newTable[string, string]() nimState.identifiers = newTable[string, string]()
nimState.currentHeader = getCurrentHeader(fullpath) nimState.currentHeader = getCurrentHeader(fullpath)
nimState.constStr &= &" {nimState.currentHeader} = \"{fp}\"\n" nimState.constStr &= &"\n {nimState.currentHeader} = \"{fp}\""
root.searchAst(astTable, nimState) root.searchAst(astTable, nimState)
@ -151,13 +151,13 @@ proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) =
echo nimState.enumStr echo nimState.enumStr
if nimState.constStr.nBl: if nimState.constStr.nBl:
echo "const\n" & nimState.constStr echo &"const {nimState.constStr}\n"
if nimState.typeStr.nBl: if nimState.typeStr.nBl:
echo "type\n" & nimState.typeStr echo &"type {nimState.typeStr}\n"
if nimState.procStr.nBl: if nimState.procStr.nBl:
echo nimState.procStr echo &"{nimState.procStr}\n"
if gStateRT.debug and nimState.debugStr.nBl: if gStateRT.debug and nimState.debugStr.nBl:
echo nimState.debugStr echo nimState.debugStr

View file

@ -1,4 +1,4 @@
import sequtils, sets, tables import segfaults, sequtils, sets, tables
import regex import regex

View file

@ -24,7 +24,7 @@ proc initGrammar(): Grammar =
name = nimState.data[0].val.getIdentifier(nskConst) name = nimState.data[0].val.getIdentifier(nskConst)
if name.nBl and nimState.identifiers.addNewIdentifer(name): if name.nBl and nimState.identifiers.addNewIdentifer(name):
nimState.constStr &= &" {name}* = {val}\n" nimState.constStr &= &"\n {name}* = {val}"
)) ))
let let
@ -148,9 +148,9 @@ proc initGrammar(): Grammar =
pout = pout[0 .. ^2] pout = pout[0 .. ^2]
if tptr == "ptr " or typ != "object": if tptr == "ptr " or typ != "object":
nimState.typeStr &= &" {name}* = proc({pout}): {getPtrType(tptr&typ)} {{.nimcall.}}\n" nimState.typeStr &= &"\n {name}* = proc({pout}): {getPtrType(tptr&typ)} {{.nimcall.}}"
else: else:
nimState.typeStr &= &" {name}* = proc({pout}) {{.nimcall.}}\n" nimState.typeStr &= &"\n {name}* = proc({pout}) {{.nimcall.}}"
else: else:
if i < nimState.data.len and nimState.data[i].name in ["identifier", "number_literal"]: if i < nimState.data.len and nimState.data[i].name in ["identifier", "number_literal"]:
var var
@ -158,12 +158,12 @@ proc initGrammar(): Grammar =
if nimState.data[i].name == "identifier": if nimState.data[i].name == "identifier":
flen = flen.getIdentifier(nskConst, name) flen = flen.getIdentifier(nskConst, name)
nimState.typeStr &= &" {name}* = {aptr}array[{flen}, {getPtrType(tptr&typ)}]\n" nimState.typeStr &= &"\n {name}* = {aptr}array[{flen}, {getPtrType(tptr&typ)}]"
else: else:
if name == typ: if name == typ:
nimState.typeStr &= &" {name}* = object\n" nimState.typeStr &= &"\n {name}* = object"
else: else:
nimState.typeStr &= &" {name}* = {getPtrType(tptr&typ)}\n" nimState.typeStr &= &"\n {name}* = {getPtrType(tptr&typ)}"
)) ))
proc pDupTypeCommon(nname: string, fend: int, nimState: NimState, isEnum=false) = proc pDupTypeCommon(nname: string, fend: int, nimState: NimState, isEnum=false) =
@ -179,11 +179,11 @@ proc initGrammar(): Grammar =
if ndname.nBl and ndname != nname: if ndname.nBl and ndname != nname:
if isEnum: if isEnum:
if nimState.identifiers.addNewIdentifer(ndname): if nimState.identifiers.addNewIdentifer(ndname):
nimState.enumStr &= &"type {ndname}* = {dptr}{nname}\n" nimState.enumStr &= &"\ntype {ndname}* = {dptr}{nname}"
else: else:
if nimState.identifiers.addNewIdentifer(ndname): if nimState.identifiers.addNewIdentifer(ndname):
nimState.typeStr &= nimState.typeStr &=
&" {ndname}* {{.importc: \"{dname}\", header: {nimState.currentHeader}, bycopy.}} = {dptr}{nname}\n" &"\n {ndname}* {{.importc: \"{dname}\", header: {nimState.currentHeader}, bycopy.}} = {dptr}{nname}"
proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int, nimState: NimState) = proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int, nimState: NimState) =
var var
@ -215,9 +215,9 @@ proc initGrammar(): Grammar =
if nname.nBl and nimState.identifiers.addNewIdentifer(nname): if nname.nBl and nimState.identifiers.addNewIdentifer(nname):
if nimState.data.len == 1: if nimState.data.len == 1:
nimState.typeStr &= &" {nname}* {{.bycopy.}} = object{union}\n" nimState.typeStr &= &"\n {nname}* {{.bycopy.}} = object{union}"
else: else:
nimState.typeStr &= &" {nname}* {{.importc: \"{prefix}{name}\", header: {nimState.currentHeader}, bycopy.}} = object{union}\n" nimState.typeStr &= &"\n {nname}* {{.importc: \"{prefix}{name}\", header: {nimState.currentHeader}, bycopy.}} = object{union}"
var var
i = fstart i = fstart
@ -248,7 +248,7 @@ proc initGrammar(): Grammar =
if i+1 < nimState.data.len-fend and nimState.data[i+1].name in gEnumVals: if i+1 < nimState.data.len-fend and nimState.data[i+1].name in gEnumVals:
let let
flen = nimState.data[i+1].val.getNimExpression() flen = nimState.data[i+1].val.getNimExpression()
nimState.typeStr &= &" {fname}*: {aptr}array[{flen}, {getPtrType(fptr&ftyp)}]\n" nimState.typeStr &= &"\n {fname}*: {aptr}array[{flen}, {getPtrType(fptr&ftyp)}]"
i += 2 i += 2
elif i+1 < nimState.data.len-fend and nimState.data[i+1].name == "function_declarator": elif i+1 < nimState.data.len-fend and nimState.data[i+1].name == "function_declarator":
var var
@ -269,15 +269,15 @@ proc initGrammar(): Grammar =
if pout.len != 0 and pout[^1] == ',': if pout.len != 0 and pout[^1] == ',':
pout = pout[0 .. ^2] pout = pout[0 .. ^2]
if fptr == "ptr " or ftyp != "object": if fptr == "ptr " or ftyp != "object":
nimState.typeStr &= &" {fname}*: proc({pout}): {getPtrType(fptr&ftyp)} {{.nimcall.}}\n" nimState.typeStr &= &"\n {fname}*: proc({pout}): {getPtrType(fptr&ftyp)} {{.nimcall.}}"
else: else:
nimState.typeStr &= &" {fname}*: proc({pout}) {{.nimcall.}}\n" nimState.typeStr &= &"\n {fname}*: proc({pout}) {{.nimcall.}}"
i += 1 i += 1
else: else:
if ftyp == "object": if ftyp == "object":
nimState.typeStr &= &" {fname}*: pointer\n" nimState.typeStr &= &"\n {fname}*: pointer"
else: else:
nimState.typeStr &= &" {fname}*: {getPtrType(fptr&ftyp)}\n" nimState.typeStr &= &"\n {fname}*: {getPtrType(fptr&ftyp)}"
i += 1 i += 1
if node.tsNodeType() == "type_definition" and if node.tsNodeType() == "type_definition" and
@ -364,8 +364,7 @@ proc initGrammar(): Grammar =
name.getIdentifier(nskType) name.getIdentifier(nskType)
if nname.nBl and nimState.identifiers.addNewIdentifer(nname): if nname.nBl and nimState.identifiers.addNewIdentifer(nname):
nimState.enumStr &= &"\ntype {nname}* = distinct int" nimState.enumStr &= &"\ndefineEnum({nname})"
nimState.enumStr &= &"\nconverter enumToInt(en: {nname}): int {{.used.}} = en.int\n"
var var
i = fstart i = fstart
@ -381,7 +380,7 @@ proc initGrammar(): Grammar =
if i+1 < nimState.data.len-fend and if i+1 < nimState.data.len-fend and
nimState.data[i+1].name in gEnumVals: nimState.data[i+1].name in gEnumVals:
if fname.nBl and nimState.identifiers.addNewIdentifer(fname): if fname.nBl and nimState.identifiers.addNewIdentifer(fname):
nimState.constStr &= &" {fname}* = ({nimState.data[i+1].val.getNimExpression()}).{nname}\n" nimState.constStr &= &"\n {fname}* = ({nimState.data[i+1].val.getNimExpression()}).{nname}"
try: try:
count = nimState.data[i+1].val.parseInt() + 1 count = nimState.data[i+1].val.parseInt() + 1
except: except:
@ -389,7 +388,7 @@ proc initGrammar(): Grammar =
i += 2 i += 2
else: else:
if fname.nBl and nimState.identifiers.addNewIdentifer(fname): if fname.nBl and nimState.identifiers.addNewIdentifer(fname):
nimState.constStr &= &" {fname}* = {count}.{nname}\n" nimState.constStr &= &"\n {fname}* = {count}.{nname}"
i += 1 i += 1
count += 1 count += 1
@ -494,9 +493,9 @@ proc initGrammar(): Grammar =
let ftyp = nimState.data[0].val.getIdentifier(nskType, fnname) let ftyp = nimState.data[0].val.getIdentifier(nskType, fnname)
if fptr == "ptr " or ftyp != "object": if fptr == "ptr " or ftyp != "object":
nimState.procStr &= &"proc {fnname}*({pout}): {getPtrType(fptr&ftyp)} {{.importc: \"{fname}\", header: {nimState.currentHeader}.}}\n" nimState.procStr &= &"\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)} {{.importc: \"{fname}\", header: {nimState.currentHeader}.}}"
else: else:
nimState.procStr &= &"proc {fnname}*({pout}) {{.importc: \"{fname}\", header: {nimState.currentHeader}.}}\n" nimState.procStr &= &"\nproc {fnname}*({pout}) {{.importc: \"{fname}\", header: {nimState.currentHeader}.}}"
)) ))

View file

@ -31,3 +31,50 @@ when defined(c):
elif defined(cpp): elif defined(cpp):
type type
wchar_t* {.importc.} = object wchar_t* {.importc.} = object
template defineEnum*(typ: untyped) =
type
typ* = distinct int
proc `+`*(x: typ, y: int): typ {.borrow.}
proc `+`*(x: int, y: typ): typ {.borrow.}
proc `+`*(x, y: typ): typ {.borrow.}
proc `-`*(x: typ, y: int): typ {.borrow.}
proc `-`*(x: int, y: typ): typ {.borrow.}
proc `-`*(x, y: typ): typ {.borrow.}
proc `*`*(x: typ, y: int): typ {.borrow.}
proc `*`*(x: int, y: typ): typ {.borrow.}
proc `*`*(x, y: typ): typ {.borrow.}
proc `<`*(x: typ, y: int): bool {.borrow.}
proc `<`*(x: int, y: typ): bool {.borrow.}
proc `<`*(x, y: typ): bool {.borrow.}
proc `<=`*(x: typ, y: int): bool {.borrow.}
proc `<=`*(x: int, y: typ): bool {.borrow.}
proc `<=`*(x, y: typ): bool {.borrow.}
proc `==`*(x: typ, y: int): bool {.borrow.}
proc `==`*(x: int, y: typ): bool {.borrow.}
proc `==`*(x, y: typ): bool {.borrow.}
proc `shl`*(x: typ, y: int): typ {.borrow.}
proc `shl`*(x: int, y: typ): typ {.borrow.}
proc `shl`*(x, y: typ): typ {.borrow.}
proc `shr`*(x: typ, y: int): typ {.borrow.}
proc `shr`*(x: int, y: typ): typ {.borrow.}
proc `shr`*(x, y: typ): typ {.borrow.}
proc `div`*(x: typ, y: int): typ {.borrow.}
proc `div`*(x: int, y: typ): typ {.borrow.}
proc `div`*(x, y: typ): typ {.borrow.}
proc `mod`*(x: typ, y: int): typ {.borrow.}
proc `mod`*(x: int, y: typ): typ {.borrow.}
proc `mod`*(x, y: typ): typ {.borrow.}
proc `$` *(x: typ): string {.borrow.}