Compare commits

...
Sign in to create a new pull request.

5 commits

Author SHA1 Message Date
Ganesh Viswanathan
619cbc5467 Minor feedback 2019-01-30 22:34:03 -06:00
Ganesh Viswanathan
b0eba35834 Suppress hint, import types in toast 2019-01-30 13:16:24 -06:00
Ganesh Viswanathan
424a4c831e Simpler implementation 2019-01-30 12:36:11 -06:00
Ganesh Viswanathan
13981e1997 Workaround gc crash on Windows 2019-01-30 11:52:13 -06:00
Ganesh Viswanathan
127abe1383 Remove enum converter - #72 2019-01-29 23:30:01 -06:00
9 changed files with 69 additions and 34 deletions

View file

@ -1,5 +1,6 @@
#[ #[
see D20190127T231316 workaround for fact that toast needs to build scanner.cc, which would otherwise result in link erros such as: see D20190127T231316 workaround for fact that toast needs to build
scanner.cc, which would otherwise result in link errors such as:
"std::terminate()", referenced from: "std::terminate()", referenced from:
___clang_call_terminate in scanner.cc.o ___clang_call_terminate in scanner.cc.o
]# ]#
@ -7,3 +8,7 @@ when defined(MacOSX):
switch("clang.linkerexe", "g++") switch("clang.linkerexe", "g++")
else: else:
switch("gcc.linkerexe", "g++") switch("gcc.linkerexe", "g++")
# Workaround for NilAccessError crash on Windows #98
when defined(Windows):
switch("gc", "markAndSweep")

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:
@ -134,6 +134,9 @@ proc printNimHeader*() =
# $2 $3 # $2 $3
{.experimental: "codeReordering".} {.experimental: "codeReordering".}
{.hint[ConvFromXtoItselfNotNeeded]: off.}
import nimterop/types
""" % [$now(), getAppFilename(), commandLineParams().join(" ")] """ % [$now(), getAppFilename(), commandLineParams().join(" ")]
proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) = proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) =
@ -143,7 +146,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 +154,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

@ -2,7 +2,7 @@ import hashes, macros, os, strformat, strutils
const CIMPORT {.used.} = 1 const CIMPORT {.used.} = 1
import "." / [globals,types,paths] import "." / [globals, types, paths]
export types export types
proc interpPath(dir: string): string= proc interpPath(dir: string): string=

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

@ -1,6 +1,6 @@
import os, strutils import os, strutils
import "."/[git,paths] import "."/[git, paths]
proc treesitterSetup*() = proc treesitterSetup*() =
gitPull("https://github.com/tree-sitter/tree-sitter/", incDir() / "treesitter", """ gitPull("https://github.com/tree-sitter/tree-sitter/", incDir() / "treesitter", """
@ -12,7 +12,7 @@ src/runtime/*
*.c *.c
*.h *.h
""") """)
# TODO: does this work on windows? if not use `os.unixToNativePath` # TODO: does this work on windows? if not use `os.unixToNativePath`
let let
stack = incDir() / "treesitter/src/runtime/stack.c" stack = incDir() / "treesitter/src/runtime/stack.c"

View file

@ -1,6 +1,6 @@
import strutils, os import strutils, os
import ".."/[setup,paths] import ".."/[setup, paths]
static: static:
treesitterCSetup() treesitterCSetup()

View file

@ -1,6 +1,6 @@
import strutils, os import strutils, os
import ".."/[setup,paths] import ".."/[setup, paths]
static: static:
treesitterCppSetup() treesitterCppSetup()

View file

@ -2,7 +2,7 @@
import strutils, os import strutils, os
import ".."/[setup,paths] import ".."/[setup, paths]
static: static:
treesitterSetup() treesitterSetup()
@ -16,7 +16,7 @@ when defined(Linux):
{.passC: "-I$1/src" % sourcePath.} {.passC: "-I$1/src" % sourcePath.}
{.passC: "-I$1/../utf8proc" % sourcePath.} {.passC: "-I$1/../utf8proc" % sourcePath.}
# pending https://github.com/nim-lang/Nim/issues/10299 we need to rename the # pending https://github.com/nim-lang/Nim/issues/10299 we need to rename the
# object files (via compile:(foo,bar)) to avoid name collisions, here # object files (via compile:(foo,bar)) to avoid name collisions, here
# and everywhere `compile` is used # and everywhere `compile` is used
{.compile: sourcePath / "src/runtime/runtime.c".} {.compile: sourcePath / "src/runtime/runtime.c".}

View file

@ -31,3 +31,31 @@ when defined(c):
elif defined(cpp): elif defined(cpp):
type type
wchar_t* {.importc.} = object wchar_t* {.importc.} = object
template enumOp*(op, typ, typout) =
proc op*(x: typ, y: int): typout {.borrow.}
proc op*(x: int, y: typ): typout {.borrow.}
proc op*(x, y: typ): typout {.borrow.}
template defineEnum*(typ) =
type
typ* = distinct int
enumOp(`+`, typ, typ)
enumOp(`-`, typ, typ)
enumOp(`*`, typ, typ)
enumOp(`<`, typ, bool)
enumOp(`<=`, typ, bool)
enumOp(`==`, typ, bool)
enumOp(`div`, typ, typ)
enumOp(`mod`, typ, typ)
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 `$` *(x: typ): string {.borrow.}