Fix Source Code Of Language

So I am making a programming language (RedStone) in Python. The syntax is similar to Python’s but a bit different. At the start of every line there should be a tilde (~).

Repl link:


~ print("Hello World!")

~ function hello
  ~ print("Hello")
~ end

~ hello()

When I run this code it always throws an error of either Invalid Syntax or bad operand type for unary ~: ‘NoneType’. (the main source code)

# too big to put here

So… Is this supposed to be hello or helloa?

Whoops typo it’s hello.

Probably don’t do:

from strings_with_arrows import *

As it’s bad practice, just use:

from strings_with_arrows import string_with_arrows

Beyond that, I don’t know what’s the actual cause of the error, as Lexers confuse me.

1 Like

The Lexer probably has nothing to do with the error here IMO.

On a different note, doesn’t run due to an unclosed parenthesis on line 170 of

1 Like

The input ~ print("Hello world") fails with an illegal character error.
Looking at Lexer.make_tokens, it will always return an error for input with a tilde.
A solution would be to check for the tilde (and any whitespace following it) and then remove it or ignore it, in the Lexer.text string.

1 Like

@NuclearPasta0 I know I need to do something like that but I don’t know how or where to put it.

In Lexer.make_tokens, have a bool variable start_tilde. Reset it every newline. After or before the newline elif case, put a new case for the tilde character that raises an error if one was already encountered this line, else set start_tilde to True. After both of those cases, put a check for start_tilde, raising an error if it is False (missing).

1 Like

Your current method doesnt work because keywords can only be lexed after the lexer encounters a letter. The best way to fix this would be to remove the tilde from every token and just consume a tilde before parsing a statement.