summary refs log tree commit diff stats
path: root/doc
Commit message (Collapse)AuthorAgeFilesLines
* doc/colorschemes: ugh, its no logical but bitwise OR!hut2010-03-171-1/+1
|
* incremented verison number v1.0.4hut2010-03-121-2/+2
|
* standardized formatting of headings in doc/hut2010-03-122-13/+31
|
* added doc/uml.txthut2010-03-121-0/+5
|
* updated pydochut2010-03-1217-161/+33
|
* updated pydochut2010-03-1270-3946/+816
|
* added two new colorschemes using 88 colorshut2010-03-121-0/+23
|
* added documentation on how colorschemes workhut2010-03-121-0/+91
|
* added symlink: doc/help => ranger/helphut2010-02-281-0/+1
|
* incremented version number and updated pydoc html files v1.0.3hut2010-02-1675-1972/+1415
|
* doc/cd-after-exit: updatedhut2010-02-141-21/+15
|
* doc/pick.sh: corrected commit orderhut2010-02-091-1/+1
|
* doc: what breaks cd-after-exit support in zshhut2010-02-091-0/+2
|
* pick.sh: added -m to checkout commadshut2010-02-051-3/+3
|
* pick.sh: added variables for easier customizationhut2010-02-041-7/+8
|
* added doc/pick.shhut2010-02-041-0/+24
|
* updated dochut2010-01-211-4/+20
|
* 1.0.2! v1.0.2hut2010-01-1430-84/+116
|
* updated pydoc documentationhut2010-01-1361-846/+795
|
* todo: added more info on bug #31hut2010-01-091-0/+5
|
* random cleanups and fixeshut2010-01-071-5/+6
|
* new minor version v1.0.1hut2010-01-022-4/+4
|
* updated pydoc documentationhut2010-01-0248-788/+3167
|
* notify: merged into statusbar, allow to view the log in the pagerhut2010-01-013-35/+2
|
* cleanupshut2009-12-311-1/+5
|
* rename filelist(container) to browsercolumn/browserviewhut2009-12-313-38/+76
|
* updated uml projecthut2009-12-305-73/+215
|
* shorten comment in ranger.pyhut2009-12-261-0/+4
|
* moved /uml to /doc/umlhut2009-12-2514-0/+2180
|
* Explained cd-after-exit featurehut2009-12-251-0/+132
|
* moved pydoc pages to doc/pydochut2009-12-2565-0/+0
|
* updated pydoc pageshut2009-12-2565-0/+10505
the previous revision' href='/ahoang/Nim/blame/compiler/nimeval.nim?h=devel&id=cae19738562f14fbb76004748bed8d2f337d6f0b'>^
06122ff71 ^
13cfaf5fd ^

06122ff71 ^



86556ebfd ^

06122ff71 ^

b30e237a3 ^
226595515 ^

06122ff71 ^

13cfaf5fd ^

06122ff71 ^
61e189792 ^
226595515 ^
06122ff71 ^



92cd07be8 ^
098a8a7c5 ^




ab872be47 ^


4301a3da9 ^
92cd07be8 ^







52829fc8d ^
4301a3da9 ^
92cd07be8 ^

ab872be47 ^
92cd07be8 ^





226595515 ^


92cd07be8 ^
226595515 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

 
                            
                                         




                                                   
                                 
      
                                          
                                        

                                             





                                                                         
                      










                                                       
                                                                          














                                                          
                                                                     








                                                                                 




                                                                    












                                                                            
                                                  





                                                       


                                                                                                








                                                               


                                                                          

                                              

                                                                

                                                       


                                                           
                           
                             
                                         
                         
                           

                                                    



                               

                                                         

                                      
                             

                                         

                     

                                                                             
               
                             
                                                                                         



                                          
 




                                                                              


                                             
                                                         







                                                         
                                               
                           

                                         
                                                              





                                                           


                                                                          
                             
                                                      
#
#
#           The Nim Compiler
#        (c) Copyright 2018 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## exposes the Nim VM to clients.
import
  ast, astalgo, modules, passes, condsyms,
  options, sem, llstream, lineinfos, vm,
  vmdef, modulegraphs, idents, os, pathutils,
  passaux, scriptconfig

type
  Interpreter* = ref object ## Use Nim as an interpreter with this object
    mainModule: PSym
    graph: ModuleGraph
    scriptName: string
    idgen: IdGenerator

iterator exportedSymbols*(i: Interpreter): PSym =
  assert i != nil
  assert i.mainModule != nil, "no main module selected"
  var it: TTabIter
  var s = initTabIter(it, i.mainModule.tab)
  while s != nil:
    yield s
    s = nextIter(it, i.mainModule.tab)

proc selectUniqueSymbol*(i: Interpreter; name: string;
                         symKinds: set[TSymKind] = {skLet, skVar}): PSym =
  ## Can be used to access a unique symbol of ``name`` and
  ## the given ``symKinds`` filter.
  assert i != nil
  assert i.mainModule != nil, "no main module selected"
  let n = getIdent(i.graph.cache, name)
  var it: TIdentIter
  var s = initIdentIter(it, i.mainModule.tab, n)
  result = nil
  while s != nil:
    if s.kind in symKinds:
      if result == nil: result = s
      else: return nil # ambiguous
    s = nextIdentIter(it, i.mainModule.tab)

proc selectRoutine*(i: Interpreter; name: string): PSym =
  ## Selects a declared routine (proc/func/etc) from the main module.
  ## The routine needs to have the export marker ``*``. The only matching
  ## routine is returned and ``nil`` if it is overloaded.
  result = selectUniqueSymbol(i, name, {skTemplate, skMacro, skFunc,
                                        skMethod, skProc, skConverter})

proc callRoutine*(i: Interpreter; routine: PSym; args: openArray[PNode]): PNode =
  assert i != nil
  result = vm.execProc(PCtx i.graph.vm, routine, args)

proc getGlobalValue*(i: Interpreter; letOrVar: PSym): PNode =
  result = vm.getGlobalValue(PCtx i.graph.vm, letOrVar)

proc implementRoutine*(i: Interpreter; pkg, module, name: string;
                       impl: proc (a: VmArgs) {.closure, gcsafe.}) =
  assert i != nil
  let vm = PCtx(i.graph.vm)
  vm.registerCallback(pkg & "." & module & "." & name, impl)

proc evalScript*(i: Interpreter; scriptStream: PLLStream = nil) =
  ## This can also be used to *reload* the script.
  assert i != nil
  assert i.mainModule != nil, "no main module selected"
  initStrTable(i.mainModule.tab)
  i.mainModule.ast = nil

  let s = if scriptStream != nil: scriptStream
          else: llStreamOpen(findFile(i.graph.config, i.scriptName), fmRead)
  processModule(i.graph, i.mainModule, i.idgen, s)

proc findNimStdLib*(): string =
  ## Tries to find a path to a valid "system.nim" file.
  ## Returns "" on failure.
  try:
    let nimexe = os.findExe("nim")
      # this can't work with choosenim shims, refs https://github.com/dom96/choosenim/issues/189
      # it'd need `nim dump --dump.format:json . | jq -r .libpath`
      # which we should simplify as `nim dump --key:libpath`
    if nimexe.len == 0: return ""
    result = nimexe.splitPath()[0] /../ "lib"
    if not fileExists(result / "system.nim"):
      when defined(unix):
        result = nimexe.expandSymlink.splitPath()[0] /../ "lib"
        if not fileExists(result / "system.nim"): return ""
  except OSError, ValueError:
    return ""

proc findNimStdLibCompileTime*(): string =
  ## Same as ``findNimStdLib`` but uses source files used at compile time,
  ## and asserts on error.
  const exe = getCurrentCompilerExe()
  result = exe.splitFile.dir.parentDir / "lib"
  doAssert fileExists(result / "system.nim"), "result:" & result

proc createInterpreter*(scriptName: string;
                        searchPaths: openArray[string];
                        flags: TSandboxFlags = {},
                        defines = @[("nimscript", "true")],
                        registerOps = true): Interpreter =
  var conf = newConfigRef()
  var cache = newIdentCache()
  var graph = newModuleGraph(cache, conf)
  connectCallbacks(graph)
  initDefines(conf.symbols)
  for define in defines:
    defineSymbol(conf.symbols, define[0], define[1])
  registerPass(graph, semPass)
  registerPass(graph, evalPass)

  for p in searchPaths:
    conf.searchPaths.add(AbsoluteDir p)
    if conf.libpath.isEmpty: conf.libpath = AbsoluteDir p

  var m = graph.makeModule(scriptName)
  incl(m.flags, sfMainModule)
  var idgen = idGeneratorFromModule(m)
  var vm = newCtx(m, cache, graph, idgen)
  vm.mode = emRepl
  vm.features = flags
  if registerOps:
    vm.registerAdditionalOps() # Required to register parts of stdlib modules
  graph.vm = vm
  graph.compileSystemModule()
  result = Interpreter(mainModule: m, graph: graph, scriptName: scriptName, idgen: idgen)

proc destroyInterpreter*(i: Interpreter) =
  ## destructor.
  discard "currently nothing to do."

proc registerErrorHook*(i: Interpreter, hook:
                        proc (config: ConfigRef; info: TLineInfo; msg: string;
                              severity: Severity) {.gcsafe.}) =
  i.graph.config.structuredErrorHook = hook

proc runRepl*(r: TLLRepl;
              searchPaths: openArray[string];
              supportNimscript: bool) =
  ## deadcode but please don't remove... might be revived
  var conf = newConfigRef()
  var cache = newIdentCache()
  var graph = newModuleGraph(cache, conf)

  for p in searchPaths:
    conf.searchPaths.add(AbsoluteDir p)
    if conf.libpath.isEmpty: conf.libpath = AbsoluteDir p

  conf.cmd = cmdInteractive # see also `setCmd`
  conf.setErrorMaxHighMaybe
  initDefines(conf.symbols)
  defineSymbol(conf.symbols, "nimscript")
  if supportNimscript: defineSymbol(conf.symbols, "nimconfig")
  when hasFFI: defineSymbol(graph.config.symbols, "nimffi")
  registerPass(graph, verbosePass)
  registerPass(graph, semPass)
  registerPass(graph, evalPass)
  var m = graph.makeStdinModule()
  incl(m.flags, sfMainModule)
  var idgen = idGeneratorFromModule(m)

  if supportNimscript: graph.vm = setupVM(m, cache, "stdin", graph, idgen)
  graph.compileSystemModule()
  processModule(graph, m, idgen, llStreamOpenStdIn(r))