From 3b1ba73ad56bdcfb1ffa39ed4c8f1dddeb607adf Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 3 Sep 2024 18:04:11 +0200 Subject: and/or types --- henshin | 69 ++++++++++++++++++++++++++++++++++++++++------------------ test/test.test | 45 +++++++++++++++++++++++++++++++------- 2 files changed, 85 insertions(+), 29 deletions(-) diff --git a/henshin b/henshin index 53611f8..7f5d755 100755 --- a/henshin +++ b/henshin @@ -18,6 +18,11 @@ parser = argparse.ArgumentParser( parser.add_argument('filename') +parser.add_argument( + '--lex', + default=False, +) + args = parser.parse_args() @@ -54,6 +59,7 @@ reserved = { "integer": "TYPE_INTEGER", "string": "TYPE_STRING", "void": "TYPE_VOID", + "type": "TYPE_TYPE", "and": "AND", "or": "OR", @@ -101,9 +107,9 @@ t_OPERATOR_PLUS = r'\+' t_OPERATOR_MINUS = r'-' t_OPERATOR_MULTIPLY = r'\*' t_OPERATOR_DIVIDE = r'/' -t_ASSIGN = '=' +t_ASSIGN = r'=' -t_OPERATOR_PIPE = r'\|>' +t_OPERATOR_PIPE = r'=>' t_OPERATOR_PIPE_REPLACEMENT = r'\$' @@ -164,17 +170,19 @@ def t_error(t): lexer = lex.lex(debug=True, debuglog=log) -lexer.input(input) -lineno = 0 -for token in lexer: - if token.lineno != lineno: - lineno = token.lineno - print("\nLine %s:" % token.lineno) +if args.lex: + lexer.input(input) + + lineno = 0 + for token in lexer: + if token.lineno != lineno: + lineno = token.lineno + print("\nLine %s:" % token.lineno) - print('%s: "%s" --' % (token.type, token.value), end=' ') - # print(token.value, end=' ') -print("\n") + print('%s: "%s" --' % (token.type, token.value), end=' ') + # print(token.value, end=' ') + print("\n") @@ -211,9 +219,8 @@ class AstNodeVariableTypes(AstNode): self.right = right class AstNodeVariableTypeArray(AstNode): - def __init__(self, type, length): + def __init__(self, type): self.type = type - self.length = length class AstNodeVariableTypeMap(AstNode): def __init__(self, key_type, value_type): @@ -353,7 +360,7 @@ def p_function_declaration_parameters(p): def p_function_declaration_parameter(p): - '''function_declaration_parameter : IDENTIFIER COLON variable_type''' + '''function_declaration_parameter : IDENTIFIER COLON variable_types''' p[0] = AstNodeFunctionDeclarationParameter(p[1], p[3]) @@ -429,16 +436,34 @@ def p_variable_declarator(p): p[0] = p[1] +def p_variable_type_primitive(p): + '''variable_type_primitive : TYPE_INTEGER + | TYPE_STRING''' + p[0] = p[1] + +def p_variable_types_primitive(p): + '''variable_types_primitive : variable_type_primitive AND variable_types_primitive + | variable_type_primitive OR variable_types_primitive + | variable_type_primitive''' + + if len(p) == 4: + p[0] = AstNodeVariableTypes(p[2], p[1], p[3]) + else: + p[0] = p[1] + + def p_variable_type(p): '''variable_type : variable_type_array | variable_type_map - | TYPE_INTEGER - | TYPE_STRING''' + | variable_type_primitive + | TYPE_TYPE + | FUNCTION''' p[0] = p[1] def p_variable_types(p): - '''variable_types : variable_type OR variable_types + '''variable_types : variable_type AND variable_types + | variable_type OR variable_types | variable_type''' if len(p) == 4: @@ -448,9 +473,9 @@ def p_variable_types(p): def p_variable_type_array(p): - '''variable_type_array : BRACKET_LEFT variable_types BRACKET_RIGHT BRACKET_LEFT NUMBER BRACKET_RIGHT''' + '''variable_type_array : BRACKET_LEFT variable_types BRACKET_RIGHT''' - p[0] = AstNodeVariableTypeArray(p[2], p[5]) + p[0] = AstNodeVariableTypeArray(p[2]) def p_variable_type_map(p): @@ -535,7 +560,7 @@ def p_map_elements(p): def p_map_element(p): - '''map_element : map_element_key COLON expression''' + '''map_element : map_element_key ASSIGN expression''' p[0] = AstNodeMapElement(p[1], p[3]) @@ -568,7 +593,9 @@ def p_expression(p): def p_error(p): - print("Syntax error in input!", p) + line_start_column = input.rfind('\n', 0, p.lexpos) + 1 + column = (p.lexpos - line_start_column) + 1 + print("Syntax error in input! Line: {line}, Column: {column}".format(line=p.lineno, column=column), p) diff --git a/test/test.test b/test/test.test index e39eae6..3778099 100644 --- a/test/test.test +++ b/test/test.test @@ -7,7 +7,7 @@ const new: integer = henshin * 5 + 10; const test: integer = 1 + 1; -function main(input: string): void { +function main(input: string or integer): void { const hello: string = "world!"; const bye: string = "bye!"; @@ -16,12 +16,12 @@ function main(input: string): void { main(input = "hello!"); -const array: [integer][3] = [1, 2, 3]; +const array: [integer] = [1, 2, 3]; const map: [string][string or integer] = [ - "first": 1, - "second": "two", - "third": "3", - "fourth": 4, + "first" = 1, + "second" = "two", + "third" = "3", + "fourth" = 4 ]; for (array) |index, value| { @@ -32,5 +32,34 @@ for (map) |key, value| { ply = key; } -main() |> print($); -main() |> main2($) |> print($); +main() => print($); +main() => main2($) => print($); + +//type test_type = { +// const test_field: string = "test", +// var another_field: integer = 4, +// +// function test_function(): void { +// return test_type.test_field; +// }, +// function_field: function = another_function(): void { +// return test_type.another_field; +// }, +//}; + +// type other_int = integer; + +//const test_type: type = {}; +//const other_int: type = integer; + +//const main: function = function(input: string): void { +// return input; +//}; + +//const main: function = (input: string): void { +// return input; +//}; + +//iterate(array, (index: integer, value: integer) { +// +//}); -- cgit v1.2.3