summary refs log tree commit diff stats
path: root/tests/iter/timplicit_auto.nim
blob: d5cb95eb8cd45f9c838ec393e40b00555761717d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# bug #1838

type State = enum Empty, Tree, Fire

const
  disp: array[State, string] = ["  ", "\e[32m/\\\e[m", "\e[07;31m/\\\e[m"]

proc univ(x, y: int): State = Tree

var w, h = 30

iterator fields(a = (0,0), b = (h-1,w-1)): auto =
  for y in max(a[0], 0) .. min(b[0], h-1):
    for x in max(a[1], 0) .. min(b[1], w-1):
      yield (y,x)

for y,x in fields():
  stdout.write disp[univ(x, y)]
#dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
discard """
  output: '''top level statements are executed!
2.0
my secret
'''
"""

## Example program that demonstrates how to use the
## compiler as an API to embed into your own projects.

import "../../compiler" / [ast, vmdef, vm, nimeval]
import std / [os]

proc main() =
  let std = findNimStdLib()
  if std.len == 0:
    quit "cannot find Nim's standard library"

  var intr = createInterpreter("myscript.nim", [std, getAppDir()])
  intr.implementRoutine("*", "exposed", "addFloats", proc (a: VmArgs) =
    setResult(a, getFloat(a, 0) + getFloat(a, 1) + getFloat(a, 2))
  )

  intr.evalScript()

  let foreignProc = selectRoutine(intr, "hostProgramRunsThis")
  if foreignProc == nil:
    quit "script does not export a proc of the name: 'hostProgramRunsThis'"
  let res = intr.callRoutine(foreignProc, [newFloatNode(nkFloatLit, 0.9),
                                           newFloatNode(nkFloatLit, 0.1)])
  if res.kind == nkFloatLit:
    echo res.floatVal
  else:
    echo "bug!"

  let foreignValue = selectUniqueSymbol(intr, "hostProgramWantsThis")
  if foreignValue == nil:
    quit "script does not export a global of the name: hostProgramWantsThis"
  let val = intr.getGlobalValue(foreignValue)
  if val.kind in {nkStrLit..nkTripleStrLit}:
    echo val.strVal
  else:
    echo "bug!"

  destroyInterpreter(intr)

main()