# # # The Nim Compiler # (c) Copyright 2015 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # # abstract syntax tree + symbol table import lineinfos, hashes, nversion, options, strutils, std / sha1, ropes, idents, intsets, idgen type TCallingConvention* = enum ccDefault, # proc has no explicit calling convention ccStdCall, # procedure is stdcall ccCDecl, # cdecl ccSafeCall, # safecall ccSysCall, # system call ccInline, # proc should be inlined ccNoInline, # proc should not be inlined ccFastCall, # fastcall (pass parameters in registers) ccClosure, # proc has a closure ccNoConvention # needed for generating proper C procs sometimes const CallingConvToStr*: array[TCallingConvention, string] = ["", "stdcall", "cdecl", "safecall", "syscall", "inline", "noinline", "fastcall", "closure", "noconv"] type TNodeKind* = enum # order is extremely important, because ranges are used # to check whether a node belongs to a certain class nkNone, # unknown node kind: indicates an error # Expressions: # Atoms: nkEmpty, # the node is empty nkIdent, # node is an identifier nkSym, # node is a symbol nkType, # node is used for its typ field nkCharLit, # a character literal '' nkIntLit, # an integer literal nkInt8Lit, nkInt16Lit, nkInt32Lit, nkInt64Lit, nkUIntLit, # an unsigned integer literal nkUInt8Lit, nkUInt16Lit, nkUInt32Lit, nkUInt64Lit, nkFloatLit, # a floating point literal nkFloat32Lit, nkFloat64Lit, nkFloat128Lit, nkStrLit, # a string literal "" nkRStrLit, # a raw string literal r"" nkTripleStrLit, # a triple string literal """ nkNilLit, # the nil literal # end of atoms nkComesFrom, # "comes from" template/macro information for # better stack trace generation nkDotCall, # used to temporarily flag a nkCall node; # this is used # for transforming ``s.len`` to ``len(s)`` nkCommand, # a call like ``p 2, 4`` without parenthesis nkCall, # a call like p(x, y) or an operation like +(a, b) nkCallStrLit, # a call with a string literal # x"abc" has two sons: nkIdent, nkRStrLit # x"""abc""" has two sons: nkIdent, nkTripleStrLit nkInfix, # a call like (a + b) nkPrefix, # a call like !a nkPostfix, # something like a! (also used for visibility) nkHiddenCallConv, # an implicit type conversion via a type converter nkExprEqExpr, # a named parameter with equals: ''expr = expr'' nkExprColonExpr, # a named parameter with colon: ''expr: expr'' nkIdentDefs, # a definition like `a, b: typeDesc = expr` # either typeDesc or expr may be nil; used in # formal parameters, var statements, etc. nkVarTuple, # a ``var (a, b) = expr`` construct nkPar, # syntactic (); may be a tuple constructor nkObjConstr, # object constructor: T(a: 1, b: 2) nkCurly, # syntactic {} nkCurlyExpr, # an expression like a{i} nkBracket, # syntactic [] nkBracketExpr, # an expression like a[i..j, k] nkPragmaExpr, # an expression like a{.pragmas.} nkRange, # an expression like i..j nkDotExpr, # a.b nkCheckedFieldExpr, # a.b, but b is a field that needs to be checked nkDerefExpr, # a^ nkIfExpr, # if as an expression nkElifExpr, nkElseExpr, nkLambda, # lambda expression nkDo, # lambda block appering as trailing proc param nkAccQuoted, # `a` as a
# Example program showing how multiple functions with the same name can
# coexist, and how we select between them.
#
# Expected output:
# 4
# 7
# 7
def test a:num -> b:num [
local-scope
load-inputs
b <- add a, 1
]
def test a:num, b:num -> c:num [
local-scope
load-inputs
c <- add a, b
]
def main [
local-scope
a:num <- test 3 # selects single-input version
$print a, 10/newline
b:num <- test 3, 4 # selects double-input version
$print b, 10/newline
c:num <- test 3, 4, 5 # prefers double- to single-input version
$print c, 10/newline
]