Skip to content
Permalink
Browse files

beginning python rewrite

the upcoming pants roadmap includes:
 * i'm going to add an abstract interpreter into the compilation pipeline for
   static analysis and optimization.
 * i'm going to stop targeting c with label pointers, which means i need to
   start outputting real functions and expect tail recursion optimization in
   the c compiler, or i should target llvm.
 * i want to fix a bunch of stuff in the parser by writing it by hand (which
   is this commit)

a lot of this stuff is serious rewrite work, and i'm sick of the visitor
design pattern. so, i'm just going to start over in python for "rapid"
prototyping.

i'll retranslate back to c with what i've learned once i'm done.

this commit provides the beginnings of a by-hand parser that implements a
bunch of stuff i punted on last time, such as
 * comments
 * multiline strings
 * string escapes
 * variable base integers and floats
 * line/column referencing
 * argument ordering
 * etc

also, it should be pretty fast due to memoization
  • Loading branch information...
jtolds committed Dec 11, 2011
1 parent f68b08e commit 5d7033d270fde6d1e5cd4a66761df88ed3253a85
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,243 @@
#!/usr/bin/env python
#
# Copyright (c) 2011, JT Olds <hello@jtolds.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

"""
Pants
http://www.pants-lang.org/
AST module
"""

__author__ = "JT Olds"
__author_email__ = "hello@jtolds.com"

class Value(object): pass

class Variable(Value):
__slots__ = ["identifier", "line", "col"]
def __init__(self, identifier, line, col):
self.identifier = identifier
self.line = line
self.col = col

class Subexpression(Value):
__slots__ = ["expressions", "line", "col"]
def __init__(self, expressions, line, col):
self.expressions = expressions
self.line = line
self.col = col

class Integer(Value):
__slots__ = ["value", "line", "col"]
def __init__(self, value, line, col):
self.value = value
self.line = line
self.col = col

class Float(Value):
__slots__ = ["value", "line", "col"]
def __init__(self, value, line, col):
self.value = value
self.line = line
self.col = col

class String(Value):
__slots__ = ["value", "line", "col", "byte_oriented"]
def __init__(self, byte_oriented, value, line, col):
self.byte_oriented = byte_oriented
self.value = value
self.line = line
self.col = col

class Array(Value):
__slots__ = ["applications", "line", "col"]
def __init__(self, applications, line, col):
self.applications = applications
self.line = line
self.col = col

class DictDefinition(object):
__slots__ = ["key", "value", "line", "col"]
def __init__(self, key, value, line, col):
self.key = key
self.value = value
self.line = line
self.col = col

class Dict(Value):
__slots__ = ["definitions", "line", "col"]
def __init__(self, definitions, line, col):
self.definitions = definitions
self.line = line
self.col = col

class Function(Value):
__slots__ = ["expressions", "left_args", "right_args", "line", "col"]
def __init__(self, expressions, left_args, right_args, line, col):
self.expressions = expressions
self.left_args = left_args
self.right_args = right_args
self.line = line
self.col = col

class Expression(object): pass

class Assignment(Expression):
__slots__ = ["mutation", "assignee", "expression", "line", "col"]
def __init__(self, mutation, assignee, expression, line, col):
self.mutation = mutation
self.assignee = assignee
self.expression = expression
self.line = line
self.col = col

class Application(Expression):
__slots__ = ["terms", "line", "col"]
def __init__(self, terms, line, col):
self.terms = terms
self.line = line
self.col = col

class Assignee(object): pass

class FieldAssignee(Assignee):
__slots__ = ["term", "field", "line", "col"]
def __init__(self, term, field, line, col):
self.term = term
self.field = field
self.line = line
self.col = col

class VariableAssignee(Assignee):
__slots__ = ["variable", "line", "col"]
def __init__(self, variable, line, col):
self.variable = variable
self.line = line
self.col = col

class IndexAssignee(Assignee):
__slots__ = ["term", "index", "line", "col"]
def __init__(self, term, index, line, col):
self.term = term
self.index = index
self.line = line
self.col = col

class Term(object):
__slots__ = ["value", "modifiers", "line", "col"]
def __init__(self, value, modifiers, line, col):
self.value = value
self.modifiers = modifiers
self.line = line
self.col = col

class ValueModifier(object): pass

class OpenCall(ValueModifier):
__slots__ = ["line", "col"]
def __init__(self, line, col):
self.line = line
self.col = col

class Index(ValueModifier):
__slots__ = ["expressions", "line", "col"]
def __init__(self, expressions, line, col):
self.expressions = expressions
self.line = line
self.col = col

class Field(ValueModifier):
__slots__ = ["identifier", "line", "col"]
def __init__(self, identifier, line, col):
self.name = identifier
self.line = line
self.col = col

class ClosedCall(ValueModifier):
__slots__ = ["left_args", "right_args", "line", "col"]
def __init__(self, left_args, right_args, line, col):
self.left_args = left_args
self.right_args = right_args
self.line = line
self.col = col

class InArgument(object): pass
class OutArgument(object): pass

class KeywordOutArgument(OutArgument):
__slots__ = ["expressions", "line", "col"]
def __init__(self, expressions, line, col):
self.expressions = expressions
self.line = line
self.col = col

class KeywordInArgument(InArgument):
__slots__ = ["identifier", "line", "col"]
def __init__(self, identifier, line, col):
self.identifier = identifier
self.line = line
self.col = col

class ArbitraryOutArgument(OutArgument):
__slots__ = ["expressions", "line", "col"]
def __init__(self, expressions, line, col):
self.expressions = expressions
self.line = line
self.col = col

class ArbitraryInArgument(InArgument):
__slots__ = ["identifier", "line", "col"]
def __init__(self, identifier, line, col):
self.identifier = identifier
self.line = line
self.col = col

class NamedOutArgument(OutArgument):
__slots__ = ["name", "value", "line", "col"]
def __init__(self, name, value, line, col):
self.name = name
self.value = value
self.line = line
self.col = col

class DefaultInArgument(InArgument):
__slots__ = ["name", "value", "line", "col"]
def __init__(self, name, value, line, col):
self.name = name
self.value = value
self.line = line
self.col = col

class PositionalOutArgument(OutArgument):
__slots__ = ["value", "line", "col"]
def __init__(self, value, line, col):
self.value = value
self.line = line
self.col = col

class RequiredInArgument(InArgument):
__slots__ = ["value", "line", "col"]
def __init__(self, value, line, col):
self.value = value
self.line = line
self.col = col
Oops, something went wrong.

0 comments on commit 5d7033d

Please sign in to comment.
You can’t perform that action at this time.