From 888dd2951742ce17e122b028a0aaf7fbe2dab5d8 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Wed, 15 Jun 2022 18:08:41 +0200 Subject: [PATCH] Print without newlines --- hello.mn | 5 ----- int.mn | 20 ++++++++++++-------- interpreter.py | 2 +- parser.py | 34 +++++++++++++++++++--------------- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/hello.mn b/hello.mn index 143d0d8..797772f 100644 --- a/hello.mn +++ b/hello.mn @@ -3,13 +3,8 @@ func: hello(string: x) = { } string: name = { - This is a multi line string - Hello world - - - } diff --git a/int.mn b/int.mn index 6a0b862..c0c777d 100644 --- a/int.mn +++ b/int.mn @@ -1,7 +1,11 @@ -func: print(number: x) = { +func: print(number: x, string: new) = { stdout: out = x + stdout: newline = new } +string: new = { + +} number: test = 5 number: another another = 5.5 @@ -13,10 +17,10 @@ number: sixth = test / another number: seventh = test % another number: eighth = 7 % test number: nineth = 7 + 5 -print(third) -print(fourth) -print(fifth) -print(sixth) -print(seventh) -print(eighth) -print(nineth) +print(third, new) +print(fourth, new) +print(fifth, new) +print(sixth, new) +print(seventh, new) +print(eighth, new) +print(nineth, new) diff --git a/interpreter.py b/interpreter.py index 04f2e36..674d9a1 100755 --- a/interpreter.py +++ b/interpreter.py @@ -84,7 +84,7 @@ for token in tokens: params[index]['signifier'] = signifier for part in function['body']: if part['type'] == "stdout": - print(parse_expression(params, part['expression'], tokens)) + print(parse_expression(params, part['expression'], tokens), end = '') case _: pass # print(token) diff --git a/parser.py b/parser.py index f089aa2..28e4711 100755 --- a/parser.py +++ b/parser.py @@ -58,7 +58,7 @@ def coaless_tokens(identifiers, line_number, usefull_tokens, variant) -> list: def collapse_tokens(uncollapsed_tokens: list) -> list: usefull_tokens = list() in_block = False - block = str() + block = "" block_index = -1 block_lineno = -1 for index in range(0, len(uncollapsed_tokens)): @@ -68,13 +68,20 @@ def collapse_tokens(uncollapsed_tokens: list) -> list: in_block = True block_lineno = token['line_number'] if token['variant'] == 'end_block': - in_block = False + compare_lineno = block_lineno + 1 + block_lineno = token['line_number'] + # Do we need to append new lines + for i in range (compare_lineno, block_lineno): + block = block + "\n" + name = "multiline" maybe_variable = peek(block_index - 2, uncollapsed_tokens) if maybe_variable['variant'] == 'signifier': name = maybe_variable['value'] usefull_token = { "variant": "variable_declaration", "signifier": name, "type": "string", "line_number": maybe_variable['line_number'], "expression": block } usefull_tokens.append(usefull_token) + # Reset for next multiline + in_block = False block = str() block_index = -1 block_lineno = -1 @@ -82,22 +89,19 @@ def collapse_tokens(uncollapsed_tokens: list) -> list: # This is a declaration if token['variant'] == 'syntax_error': if in_block: - old_lineno = block_lineno - block_lineno = token['line_number'] - if block: - for i in range (old_lineno, block_lineno -1): + compare_lineno = block_lineno + 1 + block_lineno = token['line_number'] + # This is if we don't have a block allready + if block == "": + # Do we need to prepend empty lines? + for i in range (compare_lineno, block_lineno): block = block + "\n" - block = block + token['value'] + block = block + token['value'] + "\n" else: - if old_lineno +1 != block_lineno: - for i in range (old_lineno, block_lineno -1): - block = block + "\n" - else: + # and if we do + for i in range (compare_lineno, block_lineno): block = block + "\n" - - block = block + token['value'] - for i in range (old_lineno, block_lineno -1): - block = block + "\n" + block = block + token['value'] + "\n" else: usefull_token = token