# # # The Nimrod Compiler # (c) Copyright 2013 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # # abstract syntax tree + symbol table import msgs, hashes, nversion, options, strutils, crc, ropes, idents, lists, intsets, idgen const ImportTablePos* = 0 # imported symbols are at level 0 ModuleTablePos* = 1 # module's top level symbols are at level 1 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 nkMetaNode, # difficult to explain; represents itself # (used for macros) 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 node nkTableConstr, # a table constructor {expr: expr} nkBind, # ``bind expr`` node nkClosedSymChoice, # symbol choice node; a list of nkSyms (closed) nkOpenSymChoice, # symbol choice node; a list of nkSyms (open) nkHiddenStdConv, # an implicit standard type conversion nkHiddenSubConv, # an implicit type conversion from a subtype # to a supertype nkConv, # a type conversion nkCast, # a type cast nkStaticExpr, # a static expr nkAddr, # a addr expression nkHiddenAddr, # implicit address operator nkHiddenDeref, # implicit ^ operator nkObjDownConv, # down conversion between object types nkObjUpConv, # up conversion between object types nkChckRangeF, # range check for floats nkChckRange64, # range check for 64 bit ints nkChckRange, # range check for ints nkStringToCString, # string to cstring nkCStringToString, # cstring to string # end of expressions nkAsgn, # a = b nkFastAsgn, # internal node for a fast ``a = b`` # (no string copy) nkGenericParams, # generic parameters nkFormalParams, # formal parameters nkOfInherit, # inherited from symbol nkModule, # the syntax tree of a module nkProcDef, # a proc nkMethodDef, # a method nkConverterDef, # a converter nkMacroDef, # a macro nkTemplateDef, # a template nkIteratorDef, # an iterator nkOfBranch, # used inside case statements # for (cond, action)-pairs nkElifBranch, # used in if statements nkExceptBranch, # an except section nkElse, # an else part nkAsmStmt, # an assembler block nkPragma, # a pragma statement nkPragmaBlock, # a pragma with a block nkIfStmt, # an if statement nkWhenStmt, # a when expression or statement nkForStmt, # a for statement nkParForStmt, # a parallel for statement nkWhileStmt, # a while statement nkCaseStmt, # a case statement nkTypeSection, # a type section (consists of type definitions) nkVarSection, # a var section nkLetSection, # a let section nkConstSection, # a const section nkConstDef, # a const
/* See LICENSE file for copyright and license details. */

/* appearance */
#define BORDERPX		1
#define FONT			"-*-terminus-medium-r-normal-*-14-*-*-*-*-*-*-*"
#define NORMBORDERCOLOR		"#cccccc"
#define NORMBGCOLOR		"#cccccc"
#define NORMFGCOLOR		"#000000"
#define SELBORDERCOLOR		"#0066ff"
#define SELBGCOLOR		"#0066ff"
#define SELFGCOLOR		"#ffffff"

/* bar position */
#define BX 0
#define BY 0
#define BW 1280

/* window area, including floating windows */
#define WX 0
#define WY bh
#define WW sw
#define WH sh - bh

/* master area */
#define MX WX
#define MY bh
#define MW 1280
#define MH 800 - bh

/* tile area, might be on a different screen */
#define TX 1280
#define TY 0
#define TW 1680
#define TH 1050

/* monocle area, might be restricted to a specific screen */
#define MOX MX
#define MOY MY
#define MOW MW
#define MOH MH

/* tagging */
const char tags[][MAXTAGLEN] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };

Rule rules[] = {
	/* class:instance:title substr	tags ref	isfloating */
	{ "Firefox",			tags[8],	False },
	{ "Gimp",			NULL,		True },
	{ "MPlayer",			NULL,		True },
	{ "Acroread",			NULL,		True },
};

/* layout(s) */
#define RESIZEHINTS		True	/* False - respect size hints in tiled resizals */
#define SNAP			32	/* snap pixel */

Layout layouts[] = {
	/* symbol		function	isfloating */
	{ "[]|",		tileh,		False }, /* first entry is default */
	{ "[]=",		tilev,		False },
	{ "><>",		floating,	True },
	{ "[M]",		monocle,	True },
};

/* key definitions */
#define MODKEY			Mod1Mask
Key keys[] = {
	/* modifier			key		function	argument */
#if ANSELM_OFFICE
	{ MODKEY,			XK_p,		spawn,
		"exec dmenu_run -fn '"FONT"' -nb '"NORMBGCOLOR"' -nf '"NORMFGCOLOR"' -sb '"SELBGCOLOR"' -sf '"SELFGCOLOR"' -x 0 -y 0 -w 1280" },
#else
	{ MODKEY,			XK_p,		spawn,
		"exec dmenu_run -fn '"FONT"' -nb '"NORMBGCOLOR"' -nf '"NORMFGCOLOR"' -sb '"SELBGCOLOR"' -sf '"SELFGCOLOR"'" },
#endif
	{ MODKEY|ShiftMask,		XK_Return,	spawn, "exec uxterm" },
	{ MODKEY,			XK_j,		focusnext,	NULL },
	{ MODKEY,			XK_k,		focusprev,	NULL },
	{ MODKEY,			XK_r,		reapply,	NULL },
	{ MODKEY,			XK_Return,	zoom,		NULL },
	{ MODKEY,			XK_Tab,		viewprevtag,	NULL },
	{ MODKEY,			XK_m,		setlayout,	"[M]" },
	{ MODKEY,			XK_f,		setlayout,	"><>" },
	{ MODKEY,			XK_v,		setlayout,	"[]=" },
	{ MODKEY,			XK_h,		setlayout,	"[]|" },
	{ MODKEY|ShiftMask,		XK_space,	togglefloating,	NULL },
	{ MODKEY|ShiftMask,		XK_c,		killclient,	NULL },
	{ MODKEY,			XK_0,		view,		NULL },
	{ MODKEY,			XK_1,		view,		tags[0] },
	{ MODKEY,			XK_2,		view,		tags[1] },
	{ MODKEY,			XK_3,		view,		tags[2] },
	{ MODKEY,			XK_4,		view,		tags[3] },
	{ MODKEY,			XK_5,		view,		tags[4] },
	{ MODKEY,			XK_6,		view,		tags[5] },
	{ MODKEY,			XK_7,		view,		tags[6] },
	{ MODKEY,			XK_8,		view,		tags[7] },
	{ MODKEY,			XK_9,		view,		tags[8] },
	{ MODKEY|ControlMask,		XK_1,		toggleview,	tags[0] },
	{ MODKEY|ControlMask,		XK_2,		toggleview,	tags[1] },
	{ MODKEY|ControlMask,		XK_3,		toggleview,	tags[2] },
	{ MODKEY|ControlMask,		XK_4,		toggleview,	tags[3] },
	{ MODKEY|ControlMask,		XK_5,		toggleview,	tags[4] },
	{ MODKEY|ControlMask,		XK_6,		toggleview,	tags[5] },
	{ MODKEY|ControlMask,		XK_7,		toggleview,	tags[6] },
	{ MODKEY|ControlMask,		XK_8,		toggleview,	tags[7] },
	{ MODKEY|ControlMask,		XK_9,		toggleview,	tags[8] },
	{ MODKEY|ShiftMask,		XK_0,		tag,		NULL },
	{ MODKEY|ShiftMask,		XK_1,		tag,		tags[0] },
	{ MODKEY|ShiftMask,		XK_2,		tag,		tags[1] },
	{ MODKEY|ShiftMask,		XK_3,		tag,		tags[2] },
	{ MODKEY|ShiftMask,		XK_4,		tag,		tags[3] },
	{ MODKEY|ShiftMask,		XK_5,		tag,		tags[4] },
	{ MODKEY|ShiftMask,		XK_6,		tag,		tags[5] },
	{ MODKEY|ShiftMask,		XK_7,		tag,		tags[6] },
	{ MODKEY|ShiftMask,		XK_8,		tag,		tags[7] },
	{ MODKEY|ShiftMask,		XK_9,		tag,		tags[8] },
	{ MODKEY|ControlMask|ShiftMask,	XK_1,		toggletag,	tags[0] },
	{ MODKEY|ControlMask|ShiftMask,	XK_2,		toggletag,	tags[1] },
	{ MODKEY|ControlMask|ShiftMask,	XK_3,		toggletag,	tags[2] },
	{ MODKEY|ControlMask|ShiftMask,	XK_4,		toggletag,	tags[3] },
	{ MODKEY|ControlMask|ShiftMask,	XK_5,		toggletag,	tags[4] },
	{ MODKEY|ControlMask|ShiftMask,	XK_6,		toggletag,	tags[5] },
	{ MODKEY|ControlMask|ShiftMask,	XK_7,		toggletag,	tags[6] },
	{ MODKEY|ControlMask|ShiftMask,	XK_8,		toggletag,	tags[7] },
	{ MODKEY|ControlMask|ShiftMask,	XK_9,		toggletag,	tags[8] },
	{ MODKEY|ShiftMask,		XK_q,		quit,		NULL },
};
her.sons, son) proc delSon(father: PNode, idx: int) = if isNil(father.sons): return var length = sonsLen(father) for i in countup(idx, length - 2): father.sons[i] = father.sons[i + 1] setlen(father.sons, length - 1) proc copyNode(src: PNode): PNode = # does not copy its sons! if src == nil: return nil result = newNode(src.kind) result.info = src.info result.typ = src.typ result.flags = src.flags * PersistentNodeFlags case src.Kind of nkCharLit..nkUInt64Lit: result.intVal = src.intVal of nkFloatLit..nkFloat128Lit: result.floatVal = src.floatVal of nkSym: result.sym = src.sym of nkIdent: result.ident = src.ident of nkStrLit..nkTripleStrLit: result.strVal = src.strVal else: nil proc shallowCopy*(src: PNode): PNode = # does not copy its sons, but provides space for them: if src == nil: return nil result = newNode(src.kind) result.info = src.info result.typ = src.typ result.flags = src.flags * PersistentNodeFlags case src.Kind of nkCharLit..nkUInt64Lit: result.intVal = src.intVal of nkFloatLit..nkFloat128Lit: result.floatVal = src.floatVal of nkSym: result.sym = src.sym of nkIdent: result.ident = src.ident of nkStrLit..nkTripleStrLit: result.strVal = src.strVal else: newSeq(result.sons, sonsLen(src)) proc copyTree(src: PNode): PNode = # copy a whole syntax tree; performs deep copying if src == nil: return nil result = newNode(src.kind) result.info = src.info result.typ = src.typ result.flags = src.flags * PersistentNodeFlags case src.Kind of nkCharLit..nkUInt64Lit: result.intVal = src.intVal of nkFloatLit..nkFloat128Lit: result.floatVal = src.floatVal of nkSym: result.sym = src.sym of nkIdent: result.ident = src.ident of nkStrLit..nkTripleStrLit: result.strVal = src.strVal else: newSeq(result.sons, sonsLen(src)) for i in countup(0, sonsLen(src) - 1): result.sons[i] = copyTree(src.sons[i]) proc lastSon(n: PNode): PNode = result = n.sons[sonsLen(n) - 1] proc lastSon(n: PType): PType = result = n.sons[sonsLen(n) - 1] proc hasSonWith(n: PNode, kind: TNodeKind): bool = for i in countup(0, sonsLen(n) - 1): if n.sons[i].kind == kind: return true result = false proc hasNilSon*(n: PNode): bool = for i in countup(0, safeLen(n) - 1): if n.sons[i] == nil: return true elif hasNilSon(n.sons[i]): return true result = false proc containsNode*(n: PNode, kinds: TNodeKinds): bool = if n == nil: return case n.kind of nkEmpty..nkNilLit: result = n.kind in kinds else: for i in countup(0, sonsLen(n) - 1): if n.kind in kinds or containsNode(n.sons[i], kinds): return true proc hasSubnodeWith(n: PNode, kind: TNodeKind): bool = case n.kind of nkEmpty..nkNilLit: result = n.kind == kind else: for i in countup(0, sonsLen(n) - 1): if (n.sons[i].kind == kind) or hasSubnodeWith(n.sons[i], kind): return true result = false proc replaceSons(n: PNode, oldKind, newKind: TNodeKind) = for i in countup(0, sonsLen(n) - 1): if n.sons[i].kind == oldKind: n.sons[i].kind = newKind proc sonsNotNil(n: PNode): bool = for i in countup(0, sonsLen(n) - 1): if n.sons[i] == nil: return false result = true proc getInt*(a: PNode): biggestInt = case a.kind of nkIntLit..nkUInt64Lit: result = a.intVal else: internalError(a.info, "getInt") result = 0 proc getFloat*(a: PNode): biggestFloat = case a.kind of nkFloatLit..nkFloat128Lit: result = a.floatVal else: internalError(a.info, "getFloat") result = 0.0 proc getStr*(a: PNode): string = case a.kind of nkStrLit..nkTripleStrLit: result = a.strVal else: internalError(a.info, "getStr") result = "" proc getStrOrChar*(a: PNode): string = case a.kind of nkStrLit..nkTripleStrLit: result = a.strVal of nkCharLit: result = $chr(int(a.intVal)) else: internalError(a.info, "getStrOrChar") result = "" proc isGenericRoutine*(s: PSym): bool = case s.kind of skProc, skTemplate, skMacro, skIterator, skMethod, skConverter: result = s.ast != nil and s.ast[genericParamsPos].kind != nkEmpty else: nil proc isRoutine*(s: PSym): bool {.inline.} = result = s.kind in {skProc, skTemplate, skMacro, skIterator, skMethod, skConverter} proc hasPattern*(s: PSym): bool {.inline.} = result = isRoutine(s) and s.ast.sons[patternPos].kind != nkEmpty iterator items*(n: PNode): PNode = for i in 0.. = nkNone and n.kind <= nkNilLit proc isEmptyType*(t: PType): bool {.inline.} = ## 'void' and 'stmt' types are often equivalent to 'nil' these days: result = t == nil or t.kind in {tyEmpty, tyStmt}