# I need help with my C# emulator project

So I originally decided to do this alone, but when things got complicated and my code didn't work, I decided to contact you guys and ask if you'd want to help me.

Here's the project as it currently stands now: Snap! Build Your Own Blocks

There are only two commands: the // command, better known as the comment command, and the Console.WriteLine(""); command. Basically the // command is for annotations and the Console.WriteLine(""); command can print out a statement in the console (Or in my project's case, the "Run" Tab.)

There are also a few issues, like the ScriptRunner sprite not running my Console.WriteLine(""); commands, which I don't like and don't want. I don't know how to fix this.

Also I do need to add more commands that are in the C# language.

I won't add all commands, just existing ones like "if" statements, 'void" statements (AKA functions), and other stuff. You know ;D

I do need to make the project more User-Friendly so that I can add Backspace and Enter and stuff like that so that you don't have to press the "Clear" button to clear the project only to do it again and mess up. It would be frustrating.

oh cool

Yeah but it's bad, unfinished, and terrible. I'm thinking of getting some help. Maybe you know some C# commands?

EDIT: I GOT IT WORKING AGAIN! Now I will add more commands.

Alright, I'm working on a "Console.ReadLine" command.

Console.ReadLine doesn't work! I need some help!

I know you're probably not aiming towards making an actual very functional C# interpreter/compiler/whatever it's called like I would probably do because I'm crazy, but I think it's important to keep in mind that C# is an object-oriented language, so implementing classes should be one of the first things to do when making a C# interpreter/compiler/whatever it's called. This is pretty important because programs in C# always execute from the main method in a class named Program, like so:

namespace HelloWorld {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello, world!");
}
}
}


Anyway, there is a bug. I see that you mark a line completely as a comment if it has a // anywhere in the line. Which is incorrect. It should read the characters in a line until it reaches a double slash, then skip to the next line.

Also I don't even know how you're supposed to make Console.ReadLine work without having a system to store variables and evaluate expressions, because Console.ReadLine should return a string?. I don't think your project has variables or expressions.

I understand.

Please, can you help me though? I'm stuck on commands.

Well I've made a C compiler in Snap! before. It's not the full C, it doesn't have most types or arrays or structs but I've learned a lot on how language compilers/interpreters work.

Anyway, so, for starters, you should program it to generate a list of tokens from the source code. For example, Console.WriteLine("Hello, world!"); could be:

• IDENTIFIER Console
• SYMBOL .
• IDENTIFIER WriteLine
• SYMBOL (
• STRING Hello, world!
• SYMBOL )
• SYMBOL ;

Then your program would read this list of tokens. It would read the "Console" identifier, then the "." symbol, the "WriteLine" identifier, and then the "(" symbol. From this it would conclude that this is a function call to Console.WriteLine. So after that, it reads the next token as arguments, and if the token after that is not a comma, it stops reading the arguments. Then after that is done, it expects there to be a ")" symbol. If there isn't report a syntax error. Then of course check for the semicolon.

Usually I treat this list of tokens as a stack. I can only read the first element, and when I want to move onto the next element I delete the first. This is so it's easy to keep track of the where the parser is in the token list across multiple custom blocks.

To parse expressions you can use this method. This method will create a tree data structure of the expression, accounting for operator precedence. So 1 + 3 * 5 could be turned into something like this

Yeah, but no one will help me implement that, and I can't do it either, so this project will probably be another dead project.

This project is quite dead by now. I'll postpone it until someone helps me with some code.