Exemplo n.º 1
0
"""Describes the summon for checking the history of a particular user without
using the website. Includes a link to the appropriate page on the website.
"""
from .summon import Summon
from parsing.parser import Parser
import parsing.ext_tokens
import utils.reddit_proxy
import loan_format_helper
from lbshared.responses import get_response


PARSER = Parser(
    '$check',
    [
        {'token': parsing.ext_tokens.create_user_token(), 'optional': False},
    ]
)


class CheckSummon(Summon):
    def __init__(self):
        self.name = 'check'

    def might_apply_to_comment(self, comment):
        """Determines if the $check command might be in the comment

        Returns:
            True if $check is in the comment, false otherwise
        """
        return PARSER.parse(comment['body']) is not None
Exemplo n.º 2
0
def parse(tokens: List[Token]) -> Expr:
    parser = Parser(tokens)
    return parser.expression()
Exemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(description="Esoteric C compiler")
    parser.add_argument('infiles',
                        metavar='infile',
                        type=str,
                        nargs='+',
                        help="Input files, can be either C or ASM")
    parser.add_argument('-o',
                        dest='outfile',
                        metavar='outfile',
                        type=str,
                        default='a.out',
                        required=False,
                        help="Place the output into <outfile>")
    parser.add_argument(
        '-E',
        dest='preprocess_only',
        action='store_const',
        const=True,
        default=False,
        help="Preprocess only; do not compile, assemble or link.")
    parser.add_argument('-S',
                        dest='compile_only',
                        action='store_const',
                        const=True,
                        default=False,
                        help="Compile only; do not assemble or link.")
    parser.add_argument('-c',
                        dest='assemble_only',
                        action='store_const',
                        const=True,
                        default=False,
                        help="Compile and assemble, but do not link.")
    parser.add_argument('-D',
                        dest='defines',
                        metavar='macro[=val]',
                        nargs=1,
                        action='append',
                        help='Predefine name as a macro [with value]')
    parser.add_argument('-I',
                        dest='includes',
                        metavar='path',
                        nargs=1,
                        action='append',
                        help="Path to search for unfound #include's")

    parser.add_argument('--dump-ir',
                        dest='dump_ir',
                        action='store_const',
                        const=True,
                        default=False,
                        help="Dump the IR into a file")
    parser.add_argument('--dump-ast',
                        dest='dump_ast',
                        action='store_const',
                        const=True,
                        default=False,
                        help="Dump the AST into a file")

    args = parser.parse_args()

    ################################################
    # preprocess all files
    ################################################

    preprocessor = pcpp.Preprocessor()
    preprocessor.add_path('.')
    if args.includes is not None:
        for path in args.includes:
            preprocessor.add_path(path)

    # TODO: pass defines

    #
    # Figure all the files
    #
    files = []
    asms = []
    objects = []
    for file in args.infiles:
        if file.endswith('.c'):
            preprocessor.parse(open(file), file)
            s = StringIO()
            preprocessor.write(s)
            code = s.getvalue()
            files.append((code, file))

        elif file.endswith('.S'):
            asms.append((file, open(file).read()))

        elif file.endswith('.o'):
            obj = pickle.Unpickler(open(file, 'rb')).load()
            objects.append((obj, file))

        else:
            assert False, f"Unknown file extension {file}"

    #
    # If preprocess just print the preprocessed files
    #
    if args.preprocess_only:
        for code, file in files:
            print(code)
        return

    #
    # Compile all c files
    #
    for code, file in files:
        # Parse the code into an ast
        parser = Parser(code, filename=file)
        parser.parse()
        if parser.got_errors:
            exit(1)
        assert not parser.got_errors

        # Optimize the AST
        opt = Optimizer(parser)
        opt.optimize()

        if args.dump_ast:
            with open(file[:-2] + '.ast', 'w') as f:
                for func in opt.parser.func_list:
                    f.write(str(func) + '\n')

        # Now we need to translate it into
        # the ir code
        trans = IrTranslator(parser)
        trans.translate()

        if args.dump_ir:
            with open(file[:-2] + '.ir', 'w') as f:
                p = Printer()
                for proc in trans.proc_list:
                    f.write(proc.get_name() + ":\n")
                    for inst in proc.get_body():
                        f.write('\t' + p.print_instruction(inst) + '\n')

        # Now run it through the ir translator for
        # the dcpu16
        code_trans = Dcpu16Translator()
        for proc in trans.proc_list:
            code_trans.translate_procedure(proc)
        asm = code_trans.get_asm()

        # Run the code through the peephole optimizer
        optimizer = Dcpu16PeepholeOptimizer()
        asm = optimizer.optimize(asm)

        # Add externs for any unknown label
        for func in parser.func_list:
            if func.prototype:
                asm += f'\n.extern {func.name}\n'

        # Add global vars definitions
        for var in parser.global_vars:
            if var.storage == StorageClass.EXTERN:
                asm += f'\n.extern {var.ident.name}\n'
            else:
                if var.storage != StorageClass.STATIC:
                    asm += f'\n.global {var.ident.name}\n'
                asm += f'{var.ident.name}:\n'
                if var.value is None:
                    asm += f'\t.fill {var.typ.sizeof()}, 0\n'
                else:
                    asm += f'\t.dw {var.value}\n'

        asms.append((asm, file))

    #
    # If we only do compilation then save the assembly files
    #
    if args.compile_only:
        for asm, file in asms:
            with open(file[:-2] + '.S', 'w') as f:
                f.write(asm)
        return

    #
    # Assemble all assembly files
    #
    for asm, file in asms:
        asm = Dcpu16Assembler(asm, file)
        asm.parse()
        asm.fix_labels()
        if asm.got_errors:
            exit(1)
        assert not asm.got_errors
        objects.append((asm.get_object(), file))

    #
    # If only assemble save the object files
    #
    if args.assemble_only:
        for obj, file in objects:
            pickle.Pickler(open(file[:-2] + '.o', 'wb')).dump(obj)
        return

    #
    # Link everything
    #
    linker = Dcpu16Linker()
    for obj, file in objects:
        linker.append_object(obj)
    linker.link(BinaryType.RAW)

    #
    # Output the final binary
    #
    with open(args.outfile, 'wb') as f:
        for word in linker.get_words():
            f.write(struct.pack('>H', word))
Exemplo n.º 4
0
"""Tests that we can parse $paid commands"""
import unittest
import helper  # noqa
from parsing.parser import Parser
import parsing.ext_tokens
import lbshared.money as money

try:
    from summons.paid_with_id import PARSER
except:  # noqa
    PARSER = Parser(
        ('$paid_with_id', '$paid\\_with\\_id'),
        [
            {'token': parsing.ext_tokens.create_uint_token(), 'optional': False},
            {'token': parsing.ext_tokens.create_money_token(), 'optional': False}
        ]
    )


class Test(unittest.TestCase):
    def test_simple(self):
        self.assertEqual(
            PARSER.parse('$paid_with_id 1 2'),
            [1, money.Money(200, 'USD')]
        )

    def test_escaped(self):
        self.assertEqual(
            PARSER.parse('$paid\\_with\\_id 1 2'),
            [1, money.Money(200, 'USD')]
        )
Exemplo n.º 5
0
 def test_bad_basis(self):
   parser = Parser()
   content = parser.parse_line("9374a : NODE_1952482365_2914[...,...]")
   self.assertIsNone(content)
Exemplo n.º 6
0
# Copyright (c) 2020, 2021 Jean-Sebastien Gelinas, see LICENSE at the root of the repository

import sys

from parsing.parser import Parser
from parsing.graph import Graph

file_path = sys.argv[1]
file = open(file_path)

print("Reading from '{}'".format(file_path))

parser = Parser()
graph = Graph()
try:
    stats = graph.build(parser, file, skip_until="Done building lattice!")

    print(" -> Processed {} out of {} lines (skipped {}, parsed {})".format(
        stats['nb_parsed'] + stats['nb_skipped'],
        stats['nb_read'],
        stats['nb_skipped'],
        stats['nb_parsed'],
    ))
    print(" -> Found {} nodes ({} roots, {} tails)".format(
        stats['nb_nodes'],
        stats['nb_roots'],
        stats['nb_tails'],
    ))
finally:
    file.close()
Exemplo n.º 7
0
from parsing.parser import Parser
from math_types import *
from exceptions.parsing_exceptions import *
import pytest

p = Parser()


def test1():
    inp = ("1", "+", "2", "=", "3")
    out = (Number(1), Operator("+"), Number(2), Operator("="), Number(3))
    res = p.parse(inp)
    for i, elem in enumerate(res):
        assert elem == out[i]


def test2():
    inp = ("1.5", "=", "i", "4", "**", "329.54")
    out = (Number(1.5), Operator("="), ComplexNumber(0, 1), Number(4),
           Operator("**"), Number(329.54))
    res = p.parse(inp)
    for i, elem in enumerate(res):
        assert elem == out[i]


def test3():
    inp = ("varA", "=", "[", "[", "2", ",", "3", "]", ";", "[", "4", ",", "3",
           "]", "]")
    out = (Variable("vara"), Operator("="),
           Matrix(2, 2, [[Expression([Number(2)]),
                          Expression([Number(3)])],
Exemplo n.º 8
0
they did not get any funds.
"""
from .summon import Summon
from parsing.parser import Parser
from pypika import PostgreSQLQuery as Query, Table, Parameter, Order
import parsing.ext_tokens
import utils.reddit_proxy
from lbshared.convert import convert
from lbshared.money import Money
from lbshared.responses import get_response
from lblogging import Level

PARSER = Parser('$confirm', [{
    'token': parsing.ext_tokens.create_user_token(),
    'optional': False
}, {
    'token': parsing.ext_tokens.create_money_token(),
    'optional': False
}])


class ConfirmSummon(Summon):
    def __init__(self):
        self.name = 'confirm'

    def might_apply_to_comment(self, comment):
        """Determines if the $confirm command might be in the comment

        Returns:
            True if $confirm is in the comment, false otherwise
        """
Exemplo n.º 9
0
 def evaluate(self, source_type, file_path=None):
     lexer = StdInLexer() if source_type == 'stdin' else FileLexer(
         file_path)
     parser = Parser(lexer)
     ast = parser.parse()
     return self.interpreter.interpret(ast)
Exemplo n.º 10
0
 def setUp(self) -> None:
     self.source = TestSource()
     self.lexer = TestLexer(self.source)
     self.parser = Parser(self.lexer)
Exemplo n.º 11
0
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from parsing.parser import Parser
from django.http import HttpResponse, JsonResponse
import HAB.actuation.driver_actuation as da
import HAB.pulling.driver_pulling as dp
import json

obj = Parser(filename='home.ini')
driver = da.Hada('home.ini')
pdriver = dp.Hadp('home.ini')


def index(request):
    return HttpResponse(json.dumps(obj.read()),
                        content_type='application/json')


def pulling(request):
    """
    This method basically start the data pulling
    from GPIO (or the Load status)
    This method will scrap the GPIO status
    and call the save function inside the driver
    to save the data to local database
    :param request: http request object
    :return: json response of process initiation
    """
    data = pdriver.scrap_data()
    for id in data:
        payload = pdriver.payload_creation(id, data[id])
Exemplo n.º 12
0
"""Describes the summon for marking that a particular user is delinquent. All
the loans from that user to the lender will be marked as unpaid.
"""
from .summon import Summon
from parsing.parser import Parser
import parsing.ext_tokens
import utils.reddit_proxy
from pypika import PostgreSQLQuery as Query, Table, Parameter
from pypika.functions import Now
import loan_format_helper
from lblogging import Level
from lbshared.responses import get_response
import json

PARSER = Parser('$unpaid', [{
    'token': parsing.ext_tokens.create_user_token(),
    'optional': False
}])


class UnpaidSummon(Summon):
    def __init__(self):
        self.name = 'unpaid'

    def might_apply_to_comment(self, comment):
        """Determines if the $unpaid command might be in the comment

        Returns:
            True if $unpaid is in the comment, false otherwise
        """
        return PARSER.parse(comment['body']) is not None
Exemplo n.º 13
0
def parse(file):
    parser = Parser()
    graph = Graph()
    stats = graph.build(parser, file, skip_until="Done building lattice!")
    return graph, stats