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:
___clang_call_terminate in scanner.cc.o
]#
@ -7,3 +8,7 @@ when defined(MacOSX):
switch("clang.linkerexe", "g++")
else:
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):
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:
@ -134,6 +134,9 @@ proc printNimHeader*() =
# $2 $3
{.experimental: "codeReordering".}
{.hint[ConvFromXtoItselfNotNeeded]: off.}
import nimterop/types
""" % [$now(), getAppFilename(), commandLineParams().join(" ")]
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.currentHeader = getCurrentHeader(fullpath)
nimState.constStr &= &" {nimState.currentHeader} = \"{fp}\"\n"
nimState.constStr &= &"\n {nimState.currentHeader} = \"{fp}\""
root.searchAst(astTable, nimState)
@ -151,13 +154,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

View file

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

View file

@ -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}.}}"
))

View file

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

View file

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

View file

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

View file

@ -2,7 +2,7 @@
import strutils, os
import ".."/[setup,paths]
import ".."/[setup, paths]
static:
treesitterSetup()
@ -16,7 +16,7 @@ when defined(Linux):
{.passC: "-I$1/src" % sourcePath.}
{.passC: "-I$1/../utf8proc" % sourcePath.}
# 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
{.compile: sourcePath / "src/runtime/runtime.c".}

View file

@ -31,3 +31,31 @@ when defined(c):
elif defined(cpp):
type
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.}