Skip to content

Commit

Permalink
Nim 2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrw committed Jan 15, 2024
1 parent cba2019 commit 9063a19
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 130 deletions.
4 changes: 2 additions & 2 deletions enu.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ bin_dir = "app/_dlls"
src_dir = "src"
bin = @["enu" & lib_ext]

requires "nim >= 1.6.10",
"https://github.com/dsrw/Nim#3b33173",
requires "nim 2.0.2",
"https://github.com/dsrw/Nim#ccf10a81f",
"https://github.com/arnetheduck/nim-results#f3c666a",
"https://github.com/dsrw/godot-nim#43addc1",
"https://github.com/dsrw/model_citizen 0.19.0",
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/node_controllers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ proc find_nested_changes(parent: Change[Unit]) =
elif Removed in change.changes:
parent.item.set_global(false)

proc watch_units(self: NodeController, unit: Unit) =
proc watch_units(self: NodeController, unit: Unit) {.gcsafe.} =
unit.units.watch(unit):
if added:
change.item.fix_parents(unit)
Expand Down
8 changes: 5 additions & 3 deletions src/controllers/script_controllers/host_bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ proc get_build(self: Worker, a: VmArgs, pos: int): Build =
Build(unit)

proc get_sign(self: Worker, a: VmArgs, pos: int): Sign =
let unit = self.get_unit(a, pos)
assert not unit.is_nil and unit of Sign
Sign(unit)
let pnode = a.get_node(pos)
if pnode.kind != nkNilLit:
let unit = self.get_unit(a, pos)
assert not unit.is_nil and unit of Sign
result = Sign(unit)

proc to_node(self: Worker, unit: Unit): PNode =
if ?unit:
Expand Down
41 changes: 21 additions & 20 deletions src/controllers/script_controllers/host_bridge_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,26 @@ macro bridged_from_vm(self: Worker,
arg_nodes = proc_impl[3][1..^1]

let args = collect:
var pos = -1
for ident_def in arg_nodes:
let typ = ident_def[1].str_val
if typ == $Worker.type:
ident"script_engine"
elif typ == "VmArgs":
ident"a"
elif typ == "ScriptCtx":
quote do: script_engine.active_unit.script_ctx
elif typ in ["Unit", "Bot", "Build", "Sign"]:
let getter = "get_" & typ
pos.inc
new_call(bind_sym(getter), ident"script_engine",
ident"a", new_lit(pos))

else:
let getter = "get_" & typ
pos.inc
new_call(bind_sym(getter), ident"a", new_lit(pos))
block:
var pos = -1
for ident_def in arg_nodes:
let typ = ident_def[1].str_val
if typ == $Worker.type:
ident"script_engine"
elif typ == "VmArgs":
ident"a"
elif typ == "ScriptCtx":
quote do: script_engine.active_unit.script_ctx
elif typ in ["Unit", "Bot", "Build", "Sign"]:
let getter = "get_" & typ
pos.inc
new_call(bind_sym(getter), ident"script_engine",
ident"a", new_lit(pos))

else:
let getter = "get_" & typ
pos.inc
new_call(bind_sym(getter), ident"a", new_lit(pos))

var call = new_call(proc_ref, args)
if return_node.kind == nnk_sym:
Expand All @@ -96,7 +97,7 @@ macro bridged_from_vm(self: Worker,

result.add quote do:
mixin implement_routine
`self`.interpreter.implement_routine "*", `module_name`, `proc_impl_name`,
`self`.interpreter.implement_routine "enu", `module_name`, `proc_impl_name`,
proc(a {.inject.}: VmArgs) {.gcsafe.} =

debug "calling routine", name = `proc_name`
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/script_controllers/worker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ proc watch_units(self: Worker,
parent: Unit,
body: proc(unit: Unit, change: Change[Unit], added: bool,
removed: bool) {.gcsafe.}
) =
) {.gcsafe.} =

units.track proc(changes: seq[Change[Unit]]) =
units.track proc(changes: seq[Change[Unit]]) {.gcsafe.} =
for change in changes:
let unit = change.item
let added = Added in change.changes
Expand Down
2 changes: 1 addition & 1 deletion src/game.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std / [monotimes, os, jsonutils, json, math, locks, random, net]
import std / [monotimes, os, json, math, random, net]
import pkg / [godot, metrics, metrics / stdlib_httpserver]
from dotenv import nil
import godotapi / [input, input_event, gd_os, node, scene_tree, packed_scene,
Expand Down
158 changes: 69 additions & 89 deletions src/libs/eval.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@ import compiler / [syntaxes, reorder, vmdef, msgs]
import compiler / passes {.all.}

{.warning[UnusedImport]: off.}
include compiler / [nimeval]
include compiler / [nimeval, pipelines]

export Interpreter, VmArgs, PCtx, PStackFrame, TLineInfo

# NOTE: This file is mostly made up of modified functions pulled from the nim
# compiler, and must be updated occasionally to keep up with changes to the vm.
# To make diffing easier, the original casing has been preserved, so this file
# is in `camelCase` rather than `snake_case` like the rest of the project.

# adapted from
# https://github.com/nim-lang/Nim/blob/version-1-6/compiler/passes.nim#L120
# Normal module loading procedure, but makes TPassContextArray a var param
# so it can be passed to extend_module
# https://github.com/nim-lang/Nim/blob/v2.0.2/compiler/pipelines.nim#L88
# Normal module loading procedure, but makes PContext a param so it can be
# passed to extend_module
proc processModule*(graph: ModuleGraph; module: PSym; idgen: IdGenerator;
stream: PLLStream, a: var TPassContextArray): bool {.discardable.} =
stream: PLLStream, ctx: var PContext): bool {.discardable.} =

if graph.stopCompile(): return true
let bModule = setupEvalGen(graph, module, idgen)

var
p: Parser
s: PLLStream
fileIdx = module.fileIdx

prepareConfigNotes(graph, module)
openPasses(graph, a, module, idgen)
if stream == nil:
let filename = toFullPathConsiderDirty(graph.config, fileIdx)
s = llStreamOpen(filename, fmRead)
Expand All @@ -29,62 +37,42 @@ proc processModule*(graph: ModuleGraph; module: PSym; idgen: IdGenerator;
else:
s = stream

when defined(nimsuggest):
let filename = toFullPathConsiderDirty(graph.config, fileIdx).string
msgs.setHash(graph.config, fileIdx, $sha1.secureHashFile(filename))

while true:
openParser(p, fileIdx, s, graph.cache, graph.config)
syntaxes.openParser(p, fileIdx, s, graph.cache, graph.config)

if not belongsToStdlib(graph, module) or (belongsToStdlib(graph, module) and module.name.s == "distros"):
# XXX what about caching? no processing then? what if I change the
# modules to include between compilation runs? we'd need to track that
# in ROD files. I think we should enable this feature only
# for the interactive mode.
if module.name.s != "nimscriptapi":
processImplicits graph, graph.config.implicitImports, nkImportStmt, a, module
processImplicits graph, graph.config.implicitIncludes, nkIncludeStmt, a, module
processImplicitImports graph, graph.config.implicitImports, nkImportStmt, module, ctx, bModule, idgen
processImplicitImports graph, graph.config.implicitIncludes, nkIncludeStmt, module, ctx, bModule, idgen

while true:
if graph.stopCompile(): break
checkFirstLineIndentation(p)
block processCode:
if graph.stopCompile(): break processCode
var n = parseTopLevelStmt(p)
if n.kind == nkEmpty: break
if (sfSystemModule notin module.flags and
({sfNoForward, sfReorder} * module.flags != {} or
codeReordering in graph.config.features)):
# read everything, no streaming possible
var sl = newNodeI(nkStmtList, n.info)
if n.kind == nkEmpty: break processCode
# read everything, no streaming possible
var sl = newNodeI(nkStmtList, n.info)
sl.add n
while true:
var n = parseTopLevelStmt(p)
if n.kind == nkEmpty: break
sl.add n
while true:
var n = parseTopLevelStmt(p)
if n.kind == nkEmpty: break
sl.add n
if sfReorder in module.flags or codeReordering in graph.config.features:
sl = reorder(graph, sl, module)
discard processTopLevelStmt(graph, sl, a)
break
elif n.kind in imperativeCode:
# read everything until the next proc declaration etc.
var sl = newNodeI(nkStmtList, n.info)
sl.add n
var rest: PNode = nil
while true:
var n = parseTopLevelStmt(p)
if n.kind == nkEmpty or n.kind notin imperativeCode:
rest = n
break
sl.add n
#echo "-----\n", sl
if not processTopLevelStmt(graph, sl, a): break
if rest != nil:
#echo "-----\n", rest
if not processTopLevelStmt(graph, rest, a): break
else:
#echo "----- single\n", n
if not processTopLevelStmt(graph, n, a): break

prePass(ctx, sl)
var semNode = semWithPContext(ctx, sl)
discard processPipeline(graph, semNode, bModule)

closeParser(p)
if s.kind != llsStdIn: break
closePasses(graph, a)

assert graph.pipelinePass == EvalPass
let finalNode = closePContext(graph, ctx, nil)
discard interpreterCode(bModule, finalNode)

if graph.config.backend notin {backendC, backendCpp, backendObjc}:
# We only write rod files here if no C-like backend is active.
# The C-like backends have been patched to support the IC mechanism.
Expand Down Expand Up @@ -130,8 +118,8 @@ proc resetModule*(i: Interpreter, moduleName: string) =
iface.module.ast = nil
break

proc loadModule*(i: Interpreter, fileName, code: string,
a: var TPassContextArray) {.gcsafe.} =
proc loadModule*(i: Interpreter, fileName, code: string, ctx: var PContext
) {.gcsafe.} =

assert i != nil

Expand All @@ -156,61 +144,53 @@ proc loadModule*(i: Interpreter, fileName, code: string,
# which causes "cannot evaluate at compile time" issues with some variables.
# Force things back to emRepl.
PCtx(i.graph.vm).mode = emRepl

ctx = preparePContext(i.graph, module, i.idgen)

{.gcsafe.}:
discard processModule(i.graph, module, i.idgen, stream, a)
discard processModule(i.graph, module, i.idgen, stream, ctx)

# adapted from
# https://github.com/nim-lang/Nim/blob/version-1-6/compiler/passes.nim#L120
proc extendModule(graph: ModuleGraph; a: var TPassContextArray, module: PSym;
idgen: IdGenerator; stream: PLLStream): bool {.discardable.} =
# https://github.com/nim-lang/Nim/blob/v2.0.2/compiler/pipelines.nim#L88
proc extendModule*(graph: ModuleGraph; module: PSym; idgen: IdGenerator;
stream: PLLStream, ctx: var PContext): bool {.discardable.} =

if graph.stopCompile(): return true
let bModule = setupEvalGen(graph, module, idgen)

var
p: Parser
s = stream
fileIdx = module.fileIdx

while true:
openParser(p, fileIdx, s, graph.cache, graph.config)
syntaxes.openParser(p, fileIdx, s, graph.cache, graph.config)

while true:
if graph.stopCompile(): break
checkFirstLineIndentation(p)
assert graph.pipelinePass == EvalPass
block processCode:
if graph.stopCompile(): break processCode
var n = parseTopLevelStmt(p)
if n.kind == nkEmpty: break
if (sfSystemModule notin module.flags and
({sfNoForward, sfReorder} * module.flags != {} or
codeReordering in graph.config.features)):
# read everything, no streaming possible
var sl = newNodeI(nkStmtList, n.info)
sl.add n
while true:
var n = parseTopLevelStmt(p)
if n.kind == nkEmpty: break
sl.add n

discard processTopLevelStmt(graph, sl, a)
break
elif n.kind in imperativeCode:
# read everything until the next proc declaration etc.
var sl = newNodeI(nkStmtList, n.info)
if n.kind == nkEmpty: break processCode
# read everything, no streaming possible
var sl = newNodeI(nkStmtList, n.info)
sl.add n
while true:
var n = parseTopLevelStmt(p)
if n.kind == nkEmpty: break
sl.add n
var rest: PNode = nil
while true:
var n = parseTopLevelStmt(p)
if n.kind == nkEmpty or n.kind notin imperativeCode:
rest = n
break
sl.add n
if not processTopLevelStmt(graph, sl, a): break
if rest != nil:
if not processTopLevelStmt(graph, rest, a): break
else:
if not processTopLevelStmt(graph, n, a): break

prePass(ctx, sl)

var semNode = semWithPContext(ctx, sl)
discard processPipeline(graph, semNode, bModule)

closeParser(p)
if s.kind != llsStdIn: break

result = true

proc eval*(i: Interpreter, a: var TPassContextArray, fileName, code: string) =
proc eval*(i: Interpreter, ctx: var PContext, fileName, code: string) =
## This can also be used to *reload* the script.
assert i != nil
var module: PSym
Expand All @@ -222,7 +202,7 @@ proc eval*(i: Interpreter, a: var TPassContextArray, fileName, code: string) =

assert module != nil, "no valid module selected"
let s = llStreamOpen(code)
extendModule(i.graph, a, module, i.idgen, s)
extendModule(i.graph, module, i.idgen, s, ctx)

proc config*(i: Interpreter): ConfigRef = i.graph.config

Expand Down
1 change: 1 addition & 0 deletions src/libs/interpreters.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ proc run*(self: ScriptCtx): bool =

try:
self.interpreter.load_module(self.file_name, self.code, self.pass_context)

result = false
except VMPause:
private_access ScriptCtx
Expand Down
5 changes: 2 additions & 3 deletions src/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import std / [tables, monotimes, sets, options, macros]
import godotapi / [spatial, ray_cast]
import pkg/core/godotcoretypes except Color
import pkg / core / [vector3, basis, aabb, godotbase]
import pkg / compiler / passes {.all.}
import pkg / compiler / [ast, lineinfos]
import pkg / compiler / [ast, lineinfos, semdata]
import pkg / [model_citizen]
import models / colors, libs / [eval]

Expand Down Expand Up @@ -228,7 +227,7 @@ type
interpreter*: Interpreter
code*: string
dependents*: HashSet[string]
pass_context*: TPassContextArray
pass_context*: PContext
last_ran*: MonoTime
file_index*: int

Expand Down
11 changes: 2 additions & 9 deletions tools/build_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import godotapigen
include "../installer/export_presets.cfg.nimf"
include "../installer/Info.plist.nimf"

const
stdlib = find_nim_std_lib_compile_time()
macros_url = "https://raw.githubusercontent.com/dsrw/Nim/v1.6.4-enu/lib/core/macros.nim"
const stdlib = find_nim_std_lib_compile_time()

proc core_count = echo count_processors()

Expand All @@ -34,15 +32,10 @@ proc copy_stdlib(destination: string) =
for path in @["core", "pure", "std", "fusion", "system"]:
copy_dir join_path(stdlib, path), join_path(destination, path)

for file in @["system.nim", "stdlib.nimble", "compilation.nim"]:
for file in @["system.nim", "stdlib.nimble", "system" / "compilation.nim"]:
copy_file join_path(stdlib, file),
join_path(destination, file)

when (NimMajor, NimMinor) < (1, 7):
var client = new_http_client()
let macros_source = client.get_content(macros_url)
write_file destination / "core" / "macros.nim", macros_source

proc run_tests =
discard

Expand Down

0 comments on commit 9063a19

Please sign in to comment.