Skip to content

Commit

Permalink
Merge pull request #3 from myrma/parser
Browse files Browse the repository at this point in the history
Changement de l'architecture du parser
  • Loading branch information
loristns committed May 26, 2016
2 parents 9380a31 + e53e868 commit 0e2a39b
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 239 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Pour télécharger PyAcid, il vous suffit de cloner le dépot GitHub:
git clone https://github.com/Acid-ZdS/PyAcid.git
```

Si vous ne voulez pas télécharger git, vous pouvez télécharger une archive ZIP.

### Essayer PyAcid

Afin d'exécuter PyAcid, il faut vous rendre dans le dossier que vous avez
Expand Down
5 changes: 3 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ Vous trouverez ici ce qui a été implémenté, et ce qu'il reste à coder.

Les fonctionnalités restant à implémenter dans Acid.

- Traduction en AST Python
- Compilation à partir de l'AST Python obtenu
- Créer un algorithme pour vérifier si les variables utilisés à un endroit du
code ont bien été déclarés quelque part (pour éviter les erreurs comme NameErorr
à l'exécution comme en Python)
- Traduction en AST Python
- Compilation à partir de l'AST Python obtenu
- *type-checker* statique ? (qui signale les erreurs à la compilation)
- Algorithme de *constant-folding*

## DONE

Expand Down
23 changes: 16 additions & 7 deletions acid/__main__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#!/usr/bin/env python3.4
# coding: utf-8

"""
Entry for the Acid parser/lexer/compiler/interpreter.
Contributors: myrma
"""

import os
import argparse

from acid import tokenize, parse


arg_parser = argparse.ArgumentParser(
prog='Acid compiler',
prog='acid',
description="Tokenize, parse, compile or execute the given input file"
)
arg_parser.add_argument('path', metavar='PATH', help='the input file')
Expand All @@ -19,23 +26,25 @@
help='compile the given input file')


def main(path, lex=False, ast=False, compile=True):
def main(path, lex=False, ast=False, compile=False):
with open(path) as file:
code = file.read()

if lex:
for token in tokenize(code):
print(token)

if ast:
tree = parse(code, path)
elif ast:
tree = parse(code, os.path.abspath(path))
print(tree)

if compile:
elif compile:
raise NotImplementedError('Compiling is not implemented yet')

# when the interpreter will be implemented
# execute(code)
else:
raise NotImplementedError('The interpreter is not implemented yet')
# when the interpreter will be implemented
# execute(code)

if __name__ == '__main__':
args = arg_parser.parse_args()
Expand Down
117 changes: 117 additions & 0 deletions acid/ast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env python3.4
# coding: utf-8

"""
Defines the AST structure of programs and expressions.
Contributors: myrma
"""

__all__ = [
'Program', # program AST
'Expr', 'Literal', # abstract AST nodes
'Call', 'Lambda', 'Declaration', # calls
'Variable', 'IntLiteral', 'FloatLiteral', # atoms
]


class Program:
"""
Represents a sequence of instructions.
"""

def __init__(self, instructions, path=None):
self.path = path
self.instructions = instructions

def __repr__(self):
fmt = 'Program(path={0.path!r}, instructions={0.instructions})'
return fmt.format(self)


class Expr:
"""
Abstract AST element.
"""


class Call(Expr):
"""
Function call.
ex: `(func x y z)`
"""

def __init__(self, name, args):
self.name = name
self.args = args

def __repr__(self):
return 'Call(name={0.name!r}, args={0.args})'.format(self)


class Lambda(Expr):
"""
Lambda function definition.
ex: `(lambda (x y) (+ x y))`
"""

def __init__(self, params, body):
self.params = params
self.body = body

def __repr__(self):
return 'Lambda(params={0.params!r}, body={0.body!r})'.format(self)


class Declaration(Expr):
"""
Lambda function definition.
ex: `(lambda (x y) (+ x y))`
"""

def __init__(self, name, value):
self.name = name
self.value = value

def __repr__(self):
return 'Declaration(name={0.name!r}, value={0.value!r})'.format(self)


class Variable(Expr):
"""
Variable name.
ex: `pi`
"""

def __init__(self, name):
self.name = name

def __repr__(self):
return 'Variable(name={0.name!r})'.format(self)


class Literal(Expr):
"""
Abstract literal expression.
ex: `42`, `3.14`
"""

def __init__(self, value):
self.value = value

def __repr__(self):
return '{0.__class__.__name__}(value={0.value!r})'.format(self)


class IntLiteral(Literal):
"""
Integer literal expression.
ex: `42`
"""


class FloatLiteral(Literal):
"""
Floating point number literal expression.
ex: `3.14`
"""
2 changes: 2 additions & 0 deletions acid/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"""
This module defines some exception that may be raised during tokenizing,
parsing, or execution.
Contributors: myrma
"""


Expand Down
2 changes: 2 additions & 0 deletions acid/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

"""
Defines some types and functions for tokenizing a given code string.
Contributors: myrma
"""

__all__ = ['TokenType', 'Token', 'tokenize']
Expand Down
Loading

0 comments on commit 0e2a39b

Please sign in to comment.