From 127abe1383ade2088dce062af8ffd1aa176b8d42 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Tue, 29 Jan 2019 23:30:01 -0600 Subject: [PATCH] Remove enum converter - #72 --- nimterop/ast.nim | 10 +++++----- nimterop/globals.nim | 2 +- nimterop/grammar.nim | 41 +++++++++++++++++++------------------- nimterop/types.nim | 47 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 27 deletions(-) diff --git a/nimterop/ast.nim b/nimterop/ast.nim index 0c5c4bc..b02ccfb 100644 --- a/nimterop/ast.nim +++ b/nimterop/ast.nim @@ -99,7 +99,7 @@ proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) = if searchAstForNode(ast, node, nimState): ast.tonim(ast, node, nimState) if gStateRT.debug: - nimState.debugStr &= "\n\n# " & nimState.data.join("\n# ") + nimState.debugStr &= "\n# " & nimState.data.join("\n# ") break nimState.data = @[] else: @@ -143,7 +143,7 @@ proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) = nimState.identifiers = newTable[string, string]() nimState.currentHeader = getCurrentHeader(fullpath) - nimState.constStr &= &" {nimState.currentHeader} = \"{fp}\"\n" + nimState.constStr &= &"\n {nimState.currentHeader} = \"{fp}\"" root.searchAst(astTable, nimState) @@ -151,13 +151,13 @@ proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) = echo nimState.enumStr if nimState.constStr.nBl: - echo "const\n" & nimState.constStr + echo &"const {nimState.constStr}\n" if nimState.typeStr.nBl: - echo "type\n" & nimState.typeStr + echo &"type {nimState.typeStr}\n" if nimState.procStr.nBl: - echo nimState.procStr + echo &"{nimState.procStr}\n" if gStateRT.debug and nimState.debugStr.nBl: echo nimState.debugStr \ No newline at end of file diff --git a/nimterop/globals.nim b/nimterop/globals.nim index bf9a0ef..818b0d0 100644 --- a/nimterop/globals.nim +++ b/nimterop/globals.nim @@ -1,4 +1,4 @@ -import sequtils, sets, tables +import segfaults, sequtils, sets, tables import regex diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index ad7ad9e..6b1e051 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -24,7 +24,7 @@ proc initGrammar(): Grammar = name = nimState.data[0].val.getIdentifier(nskConst) if name.nBl and nimState.identifiers.addNewIdentifer(name): - nimState.constStr &= &" {name}* = {val}\n" + nimState.constStr &= &"\n {name}* = {val}" )) let @@ -148,9 +148,9 @@ proc initGrammar(): Grammar = pout = pout[0 .. ^2] 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: - nimState.typeStr &= &" {name}* = proc({pout}) {{.nimcall.}}\n" + nimState.typeStr &= &"\n {name}* = proc({pout}) {{.nimcall.}}" else: if i < nimState.data.len and nimState.data[i].name in ["identifier", "number_literal"]: var @@ -158,12 +158,12 @@ proc initGrammar(): Grammar = if nimState.data[i].name == "identifier": 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: if name == typ: - nimState.typeStr &= &" {name}* = object\n" + nimState.typeStr &= &"\n {name}* = object" 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) = @@ -179,11 +179,11 @@ proc initGrammar(): Grammar = if ndname.nBl and ndname != nname: if isEnum: if nimState.identifiers.addNewIdentifer(ndname): - nimState.enumStr &= &"type {ndname}* = {dptr}{nname}\n" + nimState.enumStr &= &"\ntype {ndname}* = {dptr}{nname}" else: if nimState.identifiers.addNewIdentifer(ndname): 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) = var @@ -215,9 +215,9 @@ proc initGrammar(): Grammar = if nname.nBl and nimState.identifiers.addNewIdentifer(nname): if nimState.data.len == 1: - nimState.typeStr &= &" {nname}* {{.bycopy.}} = object{union}\n" + nimState.typeStr &= &"\n {nname}* {{.bycopy.}} = object{union}" 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 i = fstart @@ -248,7 +248,7 @@ proc initGrammar(): Grammar = if i+1 < nimState.data.len-fend and nimState.data[i+1].name in gEnumVals: let 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 elif i+1 < nimState.data.len-fend and nimState.data[i+1].name == "function_declarator": var @@ -269,15 +269,15 @@ proc initGrammar(): Grammar = if pout.len != 0 and pout[^1] == ',': pout = pout[0 .. ^2] 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: - nimState.typeStr &= &" {fname}*: proc({pout}) {{.nimcall.}}\n" + nimState.typeStr &= &"\n {fname}*: proc({pout}) {{.nimcall.}}" i += 1 else: if ftyp == "object": - nimState.typeStr &= &" {fname}*: pointer\n" + nimState.typeStr &= &"\n {fname}*: pointer" else: - nimState.typeStr &= &" {fname}*: {getPtrType(fptr&ftyp)}\n" + nimState.typeStr &= &"\n {fname}*: {getPtrType(fptr&ftyp)}" i += 1 if node.tsNodeType() == "type_definition" and @@ -364,8 +364,7 @@ proc initGrammar(): Grammar = name.getIdentifier(nskType) if nname.nBl and nimState.identifiers.addNewIdentifer(nname): - nimState.enumStr &= &"\ntype {nname}* = distinct int" - nimState.enumStr &= &"\nconverter enumToInt(en: {nname}): int {{.used.}} = en.int\n" + nimState.enumStr &= &"\ndefineEnum({nname})" var i = fstart @@ -381,7 +380,7 @@ proc initGrammar(): Grammar = if i+1 < nimState.data.len-fend and nimState.data[i+1].name in gEnumVals: 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: count = nimState.data[i+1].val.parseInt() + 1 except: @@ -389,7 +388,7 @@ proc initGrammar(): Grammar = i += 2 else: if fname.nBl and nimState.identifiers.addNewIdentifer(fname): - nimState.constStr &= &" {fname}* = {count}.{nname}\n" + nimState.constStr &= &"\n {fname}* = {count}.{nname}" i += 1 count += 1 @@ -494,9 +493,9 @@ proc initGrammar(): Grammar = let ftyp = nimState.data[0].val.getIdentifier(nskType, fnname) 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: - nimState.procStr &= &"proc {fnname}*({pout}) {{.importc: \"{fname}\", header: {nimState.currentHeader}.}}\n" + nimState.procStr &= &"\nproc {fnname}*({pout}) {{.importc: \"{fname}\", header: {nimState.currentHeader}.}}" )) diff --git a/nimterop/types.nim b/nimterop/types.nim index c90f2e4..bea883e 100644 --- a/nimterop/types.nim +++ b/nimterop/types.nim @@ -31,3 +31,50 @@ when defined(c): elif defined(cpp): type 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.} \ No newline at end of file