<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>examples.py</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,10 +1,29 @@
 SYMBOLS = ['do', 'defun', 'puts', 'printf', 'strlen']
+FUNCTIONS = {}
 def is_list(l):
 	return type(l) == type([])
 def is_int(n):
 	return type(n) == type(1)
 def is_atom(a):
-	return a in SYMBOLS
+	return a in SYMBOLS or a in FUNCTIONS.keys()
+
+class Function:
+	def __init__(self, args, body):
+		self.args = args
+		self.body = body
+
+class Scope:
+	def __init__(self, compiler, func):
+		self.c = compiler
+		self.func = func
+	
+	def get_arg(self, a):
+		count = 0
+		for arg in self.func.args:
+			if arg == a:
+				return ['arg', count]
+			count += 1
+		return ['atom', a]
 
 class Compiler:
 	
@@ -12,13 +31,12 @@ class Compiler:
 		self.DO_BEFORE = before
 		self.DO_AFTER = after
 		self.string_constants = {}
-		self.global_functions = {}
 		self.seq = 0
 		self.PTR_SIZE = 4
 
-	def get_arg(self, a):
+	def get_arg(self, a, scope=False):
 		if is_atom(a):
-			return ['atom', a]
+			return scope.get_arg(a)
 		if is_int(a):
 			return ['int', a]
 		if is_list(a):
@@ -32,7 +50,8 @@ class Compiler:
 		return ['strconst', seq]
 	
 	def defun(self, name, args, body):
-		self.global_functions[name] = [args, body]
+		FUNCTIONS[name] = Function(args, body)
+		return ['subexpr']
 	
 	def ifelse(self, cond, if_branch, else_branch):
 		self.compile_exp(cond)
@@ -49,13 +68,13 @@ class Compiler:
 
 
 	def output_functions(self):
-		for name, data in self.global_functions.items():
+		for name, func in FUNCTIONS.items():
 			print(&quot;.globl &quot; + str(name))
 			print(&quot;\t.type\t&quot; + str(name) + &quot;, @function&quot;)
 			print(str(name) + &quot;:&quot;)
 			print(&quot;\tpushl\t%ebp&quot;)
 			print(&quot;\tmovl\t%esp, %ebp&quot;)
-			self.compile_exp(data[1])
+			self.compile_exp(func.body, Scope(self, func))
 			print(&quot;\tleave&quot;)
 			print(&quot;\tret&quot;)
 			print(&quot;\t.size\t&quot; + str(name) + &quot;, .-&quot; + str(name))
@@ -73,29 +92,33 @@ class Compiler:
 		print(&quot;\tmovl\t$&quot; + str(name) + &quot;,%eax&quot;)
 		return ['subexpr']
 	
-	def compile_eval_arg(self, arg):
-		atype, aparam = self.get_arg(arg)
+	def compile_eval_arg(self, arg, scope=False):
+		atype, aparam = self.get_arg(arg, scope)
 		if atype == 'strconst':
 			return &quot;$.LC&quot; + str(aparam)
 		if atype == 'int':
 			return &quot;$&quot; + str(aparam)
 		if atype == 'atom':
 			return str(aparam)
+		if atype == 'arg':
+			return &quot;\tmovl\t&quot; + str(self.PTR_SIZE * (aparam + 2)) + &quot;(%ebp),%eax&quot;
 		return &quot;%eax&quot;
 	
 	def compile_call(self, func, args):
+		function = Function(args, func)
 		stack_adjustment = self.PTR_SIZE + int(round(((len(args) + 0.5) * self.PTR_SIZE / (4.0 * self.PTR_SIZE)))) * (4 * self.PTR_SIZE)
 		print(&quot;\tsubl\t$&quot; + str(stack_adjustment) + &quot;, %esp&quot;)
 		count = 0
+		scope = Scope(self, function)
 		for a in args:
-			param = self.compile_eval_arg(a)
+			param = self.compile_eval_arg(a, scope)
 			if count &gt; 0:
 				i = count * 4
 			else:
 				i = &quot;&quot;
 			print(&quot;\tmovl\t&quot; + str(param)  + &quot;,&quot; + str(i) + &quot;(%esp)&quot;)
 			count += 1
-		res = self.compile_eval_arg(func)
+		res = self.compile_eval_arg(func, scope)
 		if res == &quot;%eax&quot;:
 			res = &quot;*%eax&quot;
 		print(&quot;\tcall\t&quot; + str(res))
@@ -106,7 +129,7 @@ class Compiler:
 		map(self.compile_exp, exp)
 		return ['subexpr']
 
-	def compile_exp(self, exp):
+	def compile_exp(self, exp, scope=False):
 		if not exp or len(exp) == 0:
 			return False
 		if exp[0] == 'do':
@@ -136,7 +159,8 @@ main:
 	movl	%esp, %ebp
 	pushl	%ecx
 		&quot;&quot;&quot;)
-		self.compile_exp(exp)
+		main = Function([], [])
+		self.compile_exp(exp, Scope(self,main))
 		print(&quot;&quot;&quot;
 	popl	%ecx
 	popl	%ebp
@@ -152,21 +176,11 @@ main:
 DO_BEFORE = []
 DO_AFTER = []
 
-&quot;&quot;&quot;
-prog = ['do',
-	['if', ['strlen',&quot;&quot;],
-		['puts', &quot;IF: The string was not empty&quot;],
-		['puts', &quot;ELSE: The string was empty&quot;]
-	],
-	['if', ['strlen',&quot;Test&quot;],
-		['puts', &quot;Second IF: The string was not empty&quot;],
-		['puts', &quot;Second IF: The string was empty&quot;]
-	]
-]
-&quot;&quot;&quot;
-prog = ['do',
-	['call', ['lambda', [], [&quot;puts&quot;, &quot;Test&quot;]], [] ]
+prog = ['do', 
+	['defun', 'myputs', ['foo'], ['puts', 'foo']],
+	['myputs', &quot;TESTING MYPUTS&quot;],
 ]
 
+
 compiler = Compiler(DO_BEFORE, DO_AFTER)
 compiler.compile(prog)</diff>
      <filename>pycompiler.py</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>8fac24d017e7605c81c7d6759547d0c1725e7881</id>
    </parent>
  </parents>
  <author>
    <name>Michael Matuzak</name>
    <email>mmatuzak@gmail.com</email>
  </author>
  <url>http://github.com/emkay/pycompile/commit/6a8ab790c8361b251f881ad3a5e3406243c35138</url>
  <id>6a8ab790c8361b251f881ad3a5e3406243c35138</id>
  <committed-date>2009-05-21T15:22:01-07:00</committed-date>
  <authored-date>2009-05-21T15:22:01-07:00</authored-date>
  <message>adding arguement passing in functions.</message>
  <tree>617d25e37f2a357485fb10301d9107e8ca9bc2a0</tree>
  <committer>
    <name>Michael Matuzak</name>
    <email>mmatuzak@gmail.com</email>
  </committer>
</commit>
