So I'm making a "toy language" and also using a "toy compiler" to execute the code.
Basically, I am designing everything in C# and simply said how it works is by just making tokens from a source file, loop through them and design instructions using a list of C# Action.
I have tried "parsing/compiling" everything backwards and then reverse the Action-list when it's time to execute, which is super dumb considering how the problem is structured.
Here is the part of the source code from the "toy language"
printl("Hello, What is your name?")
string Name = inline()
printl("Oh, hello there " + Name)
And how my C# "toy compiler" goes through this is by adding actions so
printl("Hello, what is your name?")
having the string inside the function as a token with a value gives the following parsing code:
Actions.Add(new Action(() => Console.WriteLine(CurrentTok.Value)));
Though having multiple values as in the last part of the code it simply takes an empty object and adds all the values in a loop by converting the values to string until the current token becomes a ')' RightParen
Token. Resulting in a object with all the values that gets printed with the ToString()
function.
And for the one where I have the inline()
function gives the following
also keeping in mind that I have a Dictionary
of type <string, object>
to store all the variables in.
Actions.Add(new Action(() => Variables[Var_name] = Console.ReadLine()));
Now the problem comes when parsing the last line where it's supposed to write out that value, since it's already been "compiled" and the variable having no value. After the inline()
command has been executed.
The variable doesn't update it's value, since it's in a list.
Here is a simplified version of the source from the "compiler" code, for trying to explain the problem better, Note. Current = Tokens[Index]
While(Index < Tokens.Count - 1)
{ // Index a simple int
if(Ignore.Contains(CurrentTok.Type)) // Type = Type of Token
Index++ // if it's a { or a }.. and so on
if(CurrentTok.Type == TokenType.String) // TokenType = enum
{
if(Current.Value == "inline()")
{
Variables[Current.Symbol] = " "; // so it's not undefined
Actions.Add(new Action(() => Variables[Current.Symbol] = Console.ReadLine()
)); // Current.Symbol being the variable name
} else {
Variables[Current.Symbol] = Current.Value;
}
}
if(Current.Type == TokenType.Function) {
if(Current.Symbol == "printl") {
Index++;
if(Current.Type == TokenType.LParen) { // '('
Index++;
object ToPrint = " "; // init an object
While(Current.Type != TokenType.RParen) { // ')'
if(Current.Type == TokenType.Plus)
Index++;
if(Current.Type == TokenType.PrintString) {
// PrintString being a string inside a function
// that has not been declared as an variable.
object ToAdd = Current.Value;
ToPrint += Convert.ToString(ToAdd);
}
if(Current.Type == TokenType.String) {
object ToAdd = GetVar(Current.Symbol);
//GetVar = object that returns the value if the dictionary contains it
ToPrint += Convert.ToString(ToAdd);
}
Index++;
}
Actions.Add(new Action(() => Console.WriteLine(ToPrint)));
} else {
// errors...
}
}
}
index++;
}
from the source code that i listed above it works normally, it prints the text Hello, What is your name
and opens the inputstream with the readline. But returns Oh, heloo there
without the name.