Skip to content

Commit

Permalink
make nimedit compile with the new compiler API
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Jul 7, 2018
1 parent c93a2ce commit 280d6eb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 49 deletions.
7 changes: 4 additions & 3 deletions api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

# List only the imports that the main file doesn't already import here:
import
compiler/ast, compiler/vm, compiler/vmdef, compiler/msgs
compiler/ast, compiler/vm, compiler/vmdef, compiler/msgs,
nimscriptsupport

proc setupApi(result: PEvalContext; sh: SharedState) =
msgs.gErrorMax = high(int)
msgs.writelnHook = proc (msg: string) =
gConfig.errorMax = high(int)
gConfig.writelnHook = proc (msg: string) =
sh.firstWindow.console.insertReadOnly(msg & '\L')

# XXX: Expose markers.
Expand Down
58 changes: 27 additions & 31 deletions nimscriptsupport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ proc getIdent(n: PNode): int =
elif n.kind == nkSym: n.sym.name.id
else: -1

var identCache* = newIdentCache()
var gConfig* = newConfigRef()
var moduleGraph = newModuleGraph(identCache, gConfig)

var
actionsModule, colorsModule: PSym

proc getAction(x: string): PSym = strTableGet(actionsModule.tab, getIdent(x))
proc getAction(x: string): PSym = strTableGet(actionsModule.tab, getIdent(identCache, x))

proc getGlobal(varname, field: string): PNode =
let n = vm.globalCtx.getGlobalValue(getNimScriptSymbol varname)
let n = PCtx(moduleGraph.vm).getGlobalValue(getNimScriptSymbol(moduleGraph, varname))
if n.kind != nkObjConstr:
raiseVariableError(varname, "object")
for i in 1..< n.len:
for i in 1 ..< n.len:
let it = n[i]
if getIdent(it[0]) == getIdent(field).id: return it[1]
if getIdent(it[0]) == getIdent(identCache, field).id: return it[1]

proc getGlobal(varname, field: string; result: var int) =
let n = getGlobal(varname, field)
Expand Down Expand Up @@ -73,10 +77,10 @@ proc getGlobal(varname, field: string; result: var type(parseColor"")) =

proc extractStyles(result: var StyleManager; fm: var FontManager;
fontSize: byte; fontName: string) =
let n = vm.globalCtx.getGlobalValue(getNimScriptSymbol "tokens")
let n = PCtx(moduleGraph.vm).getGlobalValue(getNimScriptSymbol(moduleGraph, "tokens"))
if n.kind == nkBracket and n.len == int(high(TokenClass))+1:
for i, x in n.sons:
if x.kind == nkPar and x.len == 2 and x[0].isIntLit and x[1].isIntLit:
if x.kind in {nkTupleConstr, nkPar} and x.len == 2 and x[0].isIntLit and x[1].isIntLit:
let style = FontStyle(x[1].intVal)
result.a[TokenClass(i)] = Style(
font: fontByName(fm, fontName, fontSize, style),
Expand Down Expand Up @@ -107,52 +111,45 @@ proc detectNimLib(): string =
if not fileExists(result / "system.nim"):
quit "cannot find Nim's stdlib location"

var identCache = newIdentCache()
var moduleGraph = newModuleGraph(newConfigRef())

proc setupNimscript*(colorsScript: string): PEvalContext =
passes.gIncludeFile = includeModule
passes.gImportModule = importModule

options.libpath = detectNimLib()
add(searchPaths, options.libpath)
add(searchPaths, options.libpath / "pure")
let config = moduleGraph.config
config.libpath = detectNimLib()
add(config.searchPaths, config.libpath)
add(config.searchPaths, config.libpath / "pure")

initDefines()
defineSymbol("nimscript")
defineSymbol("nimconfig")
initDefines(config.symbols)
defineSymbol(config.symbols, "nimscript")
defineSymbol(config.symbols, "nimconfig")

registerPass(semPass)
registerPass(evalPass)
registerPass(moduleGraph, semPass)
registerPass(moduleGraph, evalPass)

colorsModule = makeModule(moduleGraph, colorsScript)
incl(colorsModule.flags, sfMainModule)
vm.globalCtx = setupVM(colorsModule, identCache, colorsScript)
compileSystemModule(moduleGraph, identCache)
result = vm.globalCtx
moduleGraph.vm = setupVM(colorsModule, identCache, colorsScript, moduleGraph)
compileSystemModule(moduleGraph)
result = PCtx(moduleGraph.vm)

proc compileActions*(actionsScript: string) =
## Compiles the actions module for the first time.
actionsModule = makeModule(moduleGraph, actionsScript)
processModule(moduleGraph, actionsModule, llStreamOpen(actionsScript, fmRead),
nil, identCache)
processModule(moduleGraph, actionsModule, llStreamOpen(actionsScript, fmRead))

proc reloadActions*(actionsScript: string) =
#resetModule(actionsModule)
processModule(moduleGraph, actionsModule, llStreamOpen(actionsScript, fmRead), nil,
identCache)
processModule(moduleGraph, actionsModule, llStreamOpen(actionsScript, fmRead))

proc execProc*(procname: string) =
let a = getAction(procname)
if a != nil:
discard vm.execProc(vm.globalCtx, a, [])
discard vm.execProc(PCtx(moduleGraph.vm), a, [])

proc supportsAction*(procname: string): bool = getAction(procname) != nil

proc runTransformator*(procname, selectedText: string): string =
let a = getAction(procname)
if a != nil:
let res = vm.execProc(vm.globalCtx, a, [newStrNode(nkStrLit, selectedText)])
let res = vm.execProc(PCtx(moduleGraph.vm), a, [newStrNode(nkStrLit, selectedText)])
if res.isStrLit:
result = res.strVal

Expand All @@ -161,8 +158,7 @@ proc loadTheme*(colorsScript: string; result: var InternalTheme;
let m = colorsModule
#resetModule(m)

processModule(moduleGraph, m, llStreamOpen(colorsScript, fmRead),
nil, identCache)
processModule(moduleGraph, m, llStreamOpen(colorsScript, fmRead))

template trivialField(field) =
getGlobal("theme", astToStr field, result.field)
Expand Down
52 changes: 37 additions & 15 deletions overviews.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,42 @@
import buffertype, buffer

import
parseutils, strutils, intsets,
compiler/ast, compiler/idents, compiler/parser,
compiler/llstream,
compiler/msgs,
compiler/astalgo, compiler/renderer, compiler/lookups
parseutils, strutils, intsets, nimscriptsupport,
compiler / [ast, idents, parser, llstream, msgs, astalgo, renderer, lookups,
lineinfos, options]

proc errorHandler(info: TLineInfo; msg: TMsgKind; arg: string) =
proc errorHandler(conf: ConfigRef; info: TLineInfo; msg: TMsgKind; arg: string) =
discard "ignore errors for the minimap generation"

proc considerQuotedIdent(n: PNode): PIdent =
case n.kind
of nkIdent: result = n.ident
of nkSym: result = n.sym.name
of nkAccQuoted:
case n.len
of 0: discard
of 1: result = considerQuotedIdent(n.sons[0])
else:
var id = ""
for i in 0..<n.len:
let x = n.sons[i]
case x.kind
of nkIdent: id.add(x.ident.s)
of nkSym: id.add(x.sym.name.s)
of nkLiterals - nkFloatLiterals: id.add(x.renderTree)
else: discard
result = getIdent(identCache, id)
of nkOpenSymChoice, nkClosedSymChoice:
if n[0].kind == nkSym:
result = n.sons[0].sym.name
else:
discard

proc allDeclarations(n: PNode; minimap: Buffer; useActiveLines: bool) =
proc addDecl(n: PNode; minimap: Buffer; useActiveLines: bool) =
if n.info.line >= 0:
if n.info.line >= 0u16:
if useActiveLines:
minimap.activeLines.incl n.info.line-1
minimap.activeLines.incl n.info.line.int-1
var nn = n
if nn.kind == nkPragmaExpr: nn = nn[0]
if nn.kind == nkPostfix: nn = nn[1]
Expand Down Expand Up @@ -53,7 +75,7 @@ proc filterMinimap*(b: Buffer) =
if b.minimapVersion != b.version:
b.minimapVersion = b.version
b.activeLines = initIntSet()
let ast = parser.parseString(b.fullText, newIdentCache(), b.filename, 0,
let ast = parser.parseString(b.fullText, identCache, gConfig, b.filename, 0,
errorHandler)
allDeclarations(ast, b, true)

Expand All @@ -63,19 +85,19 @@ proc containsIgnoreStyle(a, b: string): bool =

proc gotoNextDeclaration*(b: Buffer; ident: string): int =
var it: TIdentIter
var s = initIdentIter(it, b.symtab, getIdent(ident))
var s = initIdentIter(it, b.symtab, getIdent(identCache, ident))
if s == nil:
# there is no such declared identifier, so search for something similar:
var it: TTabIter
s = initTabIter(it, b.symtab)
while s != nil:
if s.name.s.containsIgnoreStyle(ident) and b.currentLine+1 != s.info.line:
return s.info.line
if s.name.s.containsIgnoreStyle(ident) and b.currentLine+1 != s.info.line.int:
return s.info.line.int
s = nextIter(it, b.symtab)
else:
while s != nil:
if b.currentLine+1 != s.info.line:
return s.info.line
if b.currentLine+1 != s.info.line.int:
return s.info.line.int
s = nextIdentIter(it, b.symtab)

proc populateMinimap*(minimap, buffer: Buffer) =
Expand All @@ -85,7 +107,7 @@ proc populateMinimap*(minimap, buffer: Buffer) =
minimap.filename = buffer.filename
minimap.lang = buffer.lang
# XXX make the buffer implement the streams interface
let ast = parser.parseString(buffer.fullText, newIdentCache(), buffer.filename, 0,
let ast = parser.parseString(buffer.fullText, identCache, gConfig, buffer.filename, 0,
errorHandler)
minimap.clear()
allDeclarations(ast, minimap, false)
Expand Down

0 comments on commit 280d6eb

Please sign in to comment.