Skip to content

Commit

Permalink
Add GNU header to all files, fix coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucangelio committed Aug 26, 2011
1 parent 823e788 commit b80aad7
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 23 deletions.
35 changes: 30 additions & 5 deletions constructor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# Copyright 2011 Jorge Lucangeli Obes
#
# This file is part of fj-pypy.
#
# fj-pypy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# fj-pypy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with fj-pypy. If not, see <http://www.gnu.org/licenses/>.

# Constructor objects represent class constructors.
# As constructors in FJ have a fixed structure,
# they are not represented as ordinary methods.
# Constructor objects call the constructor of the superclass,
# and then assign the object fields.
# The constructor for the Object class, ObjectConstructor,
# is the only place where objects are created.

from obj import FJObject
from util import typedNameListFromTrees, nameListFromTrees, initListFromTrees
from util import typed_name_list_from_trees, name_list_from_trees, init_list_from_trees

def constructorFromTree(class_name, root):
# ^(CONSTRUCTOR $cname $vs $ns $ins)
Expand All @@ -10,9 +35,9 @@ def constructorFromTree(class_name, root):
print "Constructor.children", [child.text for child in children]

name = children[0]
variables = typedNameListFromTrees(children[1].getChildren())
sargs = nameListFromTrees(children[2].getChildren())
inits = initListFromTrees(children[3:])
variables = typed_name_list_from_trees(children[1].getChildren())
sargs = name_list_from_trees(children[2].getChildren())
inits = init_list_from_trees(children[3:])
return Constructor(class_name, name, variables, sargs, inits)

class Constructor(object):
Expand Down Expand Up @@ -41,4 +66,4 @@ def __init__(self):
super(ObjectConstructor, self).__init__("FJObject", "FJObject", [], [], [])

def call(self, ct, params):
return FJObject("FJObject", {})
return FJObject("FJObject", {})
26 changes: 24 additions & 2 deletions expression.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# Copyright 2011 Jorge Lucangeli Obes
#
# This file is part of fj-pypy.
#
# fj-pypy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# fj-pypy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with fj-pypy. If not, see <http://www.gnu.org/licenses/>.

# Expression objects represent FJ expressions.
# Expressions appear both in methods and as the "main" part
# of FJ programs.
# Expressions can be executed, yielding an object which is the
# result of the computation of the expression.

from obj import FJObject
from util import debug_node

EXP = "EXPRESSION"
EXPS = "EXPRESSIONS"
Expand Down Expand Up @@ -126,4 +148,4 @@ def dot_exp_from_tree(root, this):
# method call
method_name = children[0].text
expressions = [exp_from_tree(child) for child in children[1].getChildren()]
return MethodCallExpression(this, method_name, expressions)
return MethodCallExpression(this, method_name, expressions)
6 changes: 4 additions & 2 deletions fj.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# You should have received a copy of the GNU General Public License
# along with fj-pypy. If not, see <http://www.gnu.org/licenses/>.

# Main executable for fj-pypy.

import sys

import antlr3
Expand All @@ -25,7 +27,7 @@
from parser.fjParser import fjParser
from parser.fjLexer import fjLexer

from klass import Class, cobject
from klass import Class, CObject
from expression import exp_from_tree
from util import debug_node

Expand Down Expand Up @@ -58,6 +60,6 @@
print "CT", ct
print "e", exp

ct["Object"] = cobject
ct["Object"] = CObject
var_dict = {}
print exp.execute(ct, var_dict)
30 changes: 27 additions & 3 deletions klass.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
# Copyright 2011 Jorge Lucangeli Obes
#
# This file is part of fj-pypy.
#
# fj-pypy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# fj-pypy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with fj-pypy. If not, see <http://www.gnu.org/licenses/>.

# Class objects represent FJ classes.
# They hold the name of the class and its parent,
# the dictionary of fields and methods,
# and the constructor for the class.

from constructor import constructorFromTree, ObjectConstructor
from method import methods_from_tree
from util import fieldsFromTree
from util import fields_from_tree

class InvalidTreeException(BaseException):
pass
Expand All @@ -26,6 +48,7 @@ def construct(self, ct, params):
@staticmethod
def fromTree(root):
# ^(CLASS $cname $pname fields constructor methods)

if root.text != Class.CLASS:
print root.text
raise InvalidTreeException
Expand All @@ -37,7 +60,7 @@ def fromTree(root):

name = children[0].text
parent = children[1].text
fields = fieldsFromTree(children[2])
fields = fields_from_tree(children[2])

# DEBUG
print "fields", fields
Expand All @@ -50,4 +73,5 @@ def fromTree(root):

return Class(parent, name, fields, constructor, methods)

cobject = Class("Object", "Object", [], ObjectConstructor(), {})
# Object class is hardcoded
CObject = Class("Object", "Object", [], ObjectConstructor(), {})
31 changes: 28 additions & 3 deletions method.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# Copyright 2011 Jorge Lucangeli Obes
#
# This file is part of fj-pypy.
#
# fj-pypy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# fj-pypy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with fj-pypy. If not, see <http://www.gnu.org/licenses/>.

# Method objects represent methods.
# Each method holds its parameters and an expression
# that represents its body.
# Methods can be executed, which means executing their expressions
# with the appropiate substitutions.

from expression import exp_from_tree
from util import typedNameListFromTrees
from util import typed_name_list_from_trees

THIS = "this"

Expand All @@ -21,19 +44,21 @@ def execute(self, this, params, ct):

def methods_from_tree(root):
# ^(METHODS method*)

children = root.getChildren()
methods = [method_from_tree(child) for child in children]
return dict([(m.name, m) for m in methods])

def method_from_tree(root):
# ^(METHOD $type $name $vs $exp)

children = root.getChildren()

# DEBUG
print "Method.children", [child.text for child in children]

type = children[0].text
name = children[1].text
args = typedNameListFromTrees(children[2].getChildren())
args = typed_name_list_from_trees(children[2].getChildren())
exp = exp_from_tree(children[3])
return Method(type, name, args, exp)
return Method(type, name, args, exp)
2 changes: 1 addition & 1 deletion obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ def __init__(self, klass, fields):
self.fields = fields

def __str__(self):
return self.klass + "(" + str(self.fields) + ")"
return self.klass + "(" + str(self.fields) + ")"
33 changes: 26 additions & 7 deletions util.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
# Copyright 2011 Jorge Lucangeli Obes
#
# This file is part of fj-pypy.
#
# fj-pypy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# fj-pypy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with fj-pypy. If not, see <http://www.gnu.org/licenses/>.

# Miscellaneous utilities.

from collections import namedtuple

TypedName = namedtuple("TypedName", "t name")
InitStatement = namedtuple("InitStatement", "l r")

def nameListFromTrees(trees):
def name_list_from_trees(trees):
names = []
for tree in trees:
names.append(tree.text)

return names

def typedNameListFromTrees(trees):
def typed_name_list_from_trees(trees):
tnames = []
for tree in trees:
children = tree.getChildren()
Expand All @@ -19,7 +38,7 @@ def typedNameListFromTrees(trees):

return tnames

def typedNameDictFromTrees(trees):
def typed_name_dict_from_trees(trees):
tnames = {}
for tree in trees:
children = tree.getChildren()
Expand All @@ -28,18 +47,18 @@ def typedNameDictFromTrees(trees):

return tnames

def initListFromTrees(trees):
def init_list_from_trees(trees):
inits = []
for tree in trees:
children = tree.getChildren()
init = InitStatement(l=children[0].text, r=children[1].text)
inits.append(init)
return inits

def fieldsFromTree(root):
def fields_from_tree(root):
fieldTrees = root.getChildren()
fields = typedNameDictFromTrees(fieldTrees)
fields = typed_name_dict_from_trees(fieldTrees)
return fields

def debug_node(node):
print node.text, [child.text for child in node.getChildren()]
print node.text, [child.text for child in node.getChildren()]

0 comments on commit b80aad7

Please sign in to comment.