 	'true': true
 	'false': false
 	'nil': null
-_context = STD_CONTEXT
+
+_context = [STD_CONTEXT]
+
+ctx = ->
+	_context[_context.length-1]
+
+getvar = (x) ->
+	for i in [_context.length-1..0]
+		if _context[i].hasOwnProperty x
+			debug "Found variable #{x} in context ##{i}"
+			return _context[i][x]
+	return undefined
+
+setvar = (x, val, overwrite_outer_scope = false) ->
+	if overwrite_outer_scope
+		for i in [_context.length-1..0]
+			if _context[i].hasOwnProperty x
+				_context[i][x] = val
+				debug "Assigned var #{x} = #{val} in context ##{i}", 2
+				return
+		err "Variable #{x} not found in any context!"
+	else
+		ctx()[x] = val
+		debug "Assigned variable #{x} = #{val} in context ##{_context.length-1}", 2
 
 # current lineno (lines start at 1)
 _lineno = 1

reset_program = ->
	_program = []
	_labels = {}
	_context = STD_CONTEXT
	_lineno = 1

# reads the whole program in a table { lineno: line }

		if fstch == '#'
			return tonumber tok[1][1..]
		if fstch == '@'
			return getvar tok[1][1..]
		if _labels[tok[1]] == undefined
			err "Label #{tok[1]} not found!"
			return
