Exemple #1
0
def primitive_values():
    return [
        (common.OP_ALTERNATIVE,
            syntax.Forall(
                var='a',
                body=syntax.function(
                    syntax.Variable(name='a'),
                    syntax.function(
                        syntax.Variable(name='a'),
                        syntax.Variable(name='a'))))),
        (common.OP_SEQUENCE,
            syntax.Forall(
                var='a',
                body=syntax.Forall(
                    var='b',
                    body=syntax.function(
                        syntax.Variable(name='a'),
                        syntax.function(
                            syntax.Variable(name='b'),
                            syntax.Variable(name='b')))))),
        (common.OP_UNIFY,
            syntax.Forall(
                var='a',
                body=syntax.function(
                    syntax.Variable(name='a'),
                    syntax.function(
                        syntax.Variable(name='a'),
                        syntax.primitive_type_unit())))),

        (common.VALUE_UNIT, syntax.primitive_type_unit()),
    ]
Exemple #2
0
 def parse_atom(self):
     position = self.current_position()
     if self._token.type() == token.LPAREN:
         self.match(token.LPAREN)
         if self._token.type() == token.RPAREN:
             self.match(token.RPAREN)
             return syntax.Variable(name="()", position=position)
         expr = self.parse_expression()
         self.match(token.RPAREN)
         return expr
     elif self._token.type() == token.NUM:
         return syntax.IntegerConstant(value=self.parse_num(),
                                       position=position)
     elif self._token.type() == token.ID:
         return syntax.Variable(name=self.parse_id(), position=position)
     elif self._token.type() == token.UNDERSCORE:
         self.next_token()
         v = syntax.fresh_variable(prefix='_', position=position)
         return syntax.Fresh(var=v.name, body=v, position=position)
     self.fail('expected-atom', got=self._token)
Exemple #3
0
 def parse_expression_infixl(self, level):
     position = self.current_position()
     next_level = self._prectable.next_level(level)
     expr = self.parse_expression_mixfix(level=next_level)
     while self.is_operator_part() and \
           self._prectable.is_binop_in_level(level, self._token.value()):
         op = lexer.operator_from_parts(['', self._token.value(), ''])
         operator = syntax.Variable(name=op, position=position)
         self.next_token()
         arg = self.parse_expression_mixfix(level=next_level)
         expr = syntax.Application(fun=syntax.Application(
             fun=operator, arg=expr, position=position),
                                   arg=arg,
                                   position=position)
     return expr
Exemple #4
0
    def desugar_definition(self, name, equations):
        position = equations[0].position
        alternatives = []

        patterns_0 = equations[0].lhs.application_args()
        params = [syntax.fresh_variable(position=position)
                    for pat in patterns_0]

        definition_type = self._env.value(name)

        self._env.open_scope() # Definition scope

        param_types = []
        for param in params:
            param_type = syntax.Metavar(prefix='t', position=position)
            self._env.define(param, param_type)
            param_types.append(param_type)
        result_type = syntax.Metavar(prefix='t', position=position)

        self.unify_types(
          definition_type,
          syntax.function_many(param_types, result_type)
        )

        for equation in equations:
            alternatives.append(
              self.desugar_equation(params, param_types, result_type, equation)
            )

        rhs = syntax.lambda_many(
                [param.name for param in params],
                syntax.alternative_many(alternatives, position=position),
                position=position,
              )
        self._env.close_scope() # Definition scope
        return syntax.Definition(
                 lhs=syntax.Variable(name=name,
                                     position=position),
                 rhs=rhs,
                 where=[],
                 position=position)
Exemple #5
0
 def parse_program(self):
     position = self.current_position()
     self.match(token.BEGIN)
     data_declarations = []
     value_declarations = []
     while self._token.type() == token.DELIM:
         self.match(token.DELIM)
         for decl in self.parse_toplevel_declaration():
             if decl.is_data_declaration():
                 data_declarations.append(decl)
             else:
                 value_declarations.append(decl)
     self.match(token.END)
     self.match(token.EOF)
     return syntax.Program(
         data_declarations=data_declarations,
         body=syntax.Let(declarations=value_declarations,
                         body=syntax.Variable(name="main",
                                              position=position),
                         position=position),
         position=position,
     )
Exemple #6
0
 def parse_expression_infix(self, level):
     position = self.current_position()
     status = []
     children = []
     while not self.end_of_expression():
         tokval = self._token.value()
         must_read_part = ((len(status) == 0 and self.is_operator_part()
                            and self._prectable.is_status_prefix_in_level(
                                level, [tokval]))
                           or (len(status) > 0 and status[-1] == ''))
         if must_read_part:
             if not self.is_operator_part() or \
                not self._prectable.is_status_prefix_in_level(
                      level,
                      status + [tokval]):
                 if len(status) == 1:
                     break
                 else:
                     self.fail('expected-operator-part',
                               status=lexer.operator_from_parts(status))
             status.append(tokval)
             self.next_token()
         else:
             status.append('')
             next_level = self._prectable.next_level(level)
             children.append(self.parse_expression_mixfix(level=next_level))
         if self._prectable.is_status_in_level(level, status):
             expr = syntax.Variable(name=lexer.operator_from_parts(status))
             for arg in children:
                 expr = syntax.Application(fun=expr,
                                           arg=arg,
                                           position=position)
             return expr
     if status == ['']:
         return children[0]
     self.fail('cannot-parse-expression')