summary refs log tree commit diff stats
path: root/compiler/semmagic.nim
blob: 6b868d19bf1f132fddd96d7b822d6d7496d562b7 (plain) (blame)
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
#
#
#           The Nimrod Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This include file implements the semantic checking for magics.

proc semIsPartOf(c: PContext, n: PNode, flags: TExprFlags): PNode =
  var r = isPartOf(n[1], n[2])
  result = newIntNodeT(ord(r), n)
  
proc semSlurp(c: PContext, n: PNode, flags: TExprFlags): PNode = 
  assert sonsLen(n) == 2
  var a = expectStringArg(c, n, 0)
  try:
    var filename = a.strVal.FindFile
    var content = readFile(filename)
    result = newStrNode(nkStrLit, content)
    result.typ = getSysType(tyString)
    result.info = n.info
    c.slurpedFiles.add(filename)
  except EIO:
    GlobalError(a.info, errCannotOpenFile, a.strVal)

proc magicsAfterOverloadResolution(c: PContext, n: PNode, 
                                   flags: TExprFlags): PNode =
  case n[0].sym.magic
  of mSlurp: result = semSlurp(c, n, flags)
  of mIsPartOf: result = semIsPartOf(c, n, flags)
  of mAstToStr:
    result = newStrNodeT(renderTree(n[1], {renderNoComments}), n)
    result.typ = getSysType(tyString)
  else: result = n