Skip to content

Instantly share code, notes, and snippets.

@Varriount
Created July 19, 2017 15:50
Show Gist options
  • Save Varriount/14294ffa001805915dcc7463d698882a to your computer and use it in GitHub Desktop.
Save Varriount/14294ffa001805915dcc7463d698882a to your computer and use it in GitHub Desktop.
# This is an example how an abstract syntax tree could be modelled in Nim
type
NodeKind = enum # the different node types
nkInt, # a leaf with an integer value
nkFloat, # a leaf with a float value
nkString, # a leaf with a string value
nkAdd, # an addition
nkSub, # a subtraction
nkIf # an if statement
Node = ref NodeObj
NodeObj = object
nodeId: int
line, column: int
case kind: NodeKind
of nkInt:
intVal: int
of nkFloat:
floatVal: float
of nkString:
strVal: string
of nkAdd, nkSub:
leftOp, rightOp: Node
of nkIf:
condition, thenPart, elsePart: Node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment