So I am currently making my own programming language based off of howCode's programming language in Python, but I simply took an hour or so to attempt to convert it into C#, and it went great, although, when I tell the parse to parse the tokens we have collected, it only parses it once after it finds a PRINT STRING in or tokens, and then just stops,
This is the code for my parser, lexer, my script for the laguage, and the console:
Parser:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BL
{
public static class Parser
{
public static void Parse(string toks)
{
if (toks.Substring(0).Split(':')[0] == "PRINT STRING")
{
Console.WriteLine(toks.Substring(toks.IndexOf('\"') + 1).Split('\"')[0]);
}
}
}
}
Lexer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BL
{
public static class Lexer
{
public static string tok = "";
public static string str;
public static int state = 0;
public static string tokens = "";
public static void Lex(string data)
{
foreach (char c in data)
{
tok += c;
if (tok == " ")
{
if (state == 0)
{
tok = "";
tokens += " ";
}
else if (state == 1)
{
tok = " ";
}
}
else if (tok == Environment.NewLine)
{
tok = "";
}
else if (tok == "PRINT")
{
tokens += "PRINT";
tok = "";
}
else if (tok == "\"")
{
if (state == 0)
{
state = 1;
}
else if (state == 1)
{
tokens += "STRING:" + str + "\" ";
str = "";
state = 0;
tok = "";
}
}
else if (state == 1)
{
str += tok;
tok = "";
}
}
Parser.Parse(tokens);
}
}
}
my Script:
PRINT "HELLO WORLD1"
PRINT "HELLO WORLD2"
the Console:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace BL
{
class Program
{
static string data;
static void Main(string[] args)
{
Console.Title = "Compiler";
string input = Console.ReadLine();
Open(input);
Lexer.Lex(data);
Console.ReadLine();
}
public static void Open(string file)
{
data = File.ReadAllText(file);
}
}
}
when I print the contents of tokens (in Lexer) I get this:
PRINT STRING:"HELLO WORLD1" PRINT STRING:"HELLO WORLD2"
although when I parse it, it only prints HELLO WORLD1, not HELLO WORLD1 and underneath it HELLO WORLD2, I'm not sure what I should do to get the other PRINT STRING, an obviously since this was a project only I have created, there is no answer online, thank you in advance.