Hi guys (and gals). I've decided to make a C interpreter in Snap!. Because it would be cool.
https://snap.berkeley.edu/snap/snap.html#present:Username=pumpkinhead&ProjectName=C%20dev%20release
This isn't the version I'm working on. It's just a copy of the latest version of my C interpreter that, as far as I know, actually works, since I'm always going to add more features when working on this.
Well as the title says I am still working on this -- most features in C are not supported yet. It took me about 4 days to reach to this point (I have summer vacation and not really anything to do). But here is a list of what is supported at the moment of writing this:
- Main function (obviously)
- Expressions
- Arithmetic (+, -, /, *), bitwise (&, |, ~, ^), and ternary (?
operators. (Dumb auto-emoji thing I hate those...)
- Only int and char types
- Type conversions (only explicit)
- User-defined functions with arguments/parameters
- Control statements (if, while, for, do)
#include
preprocessor directive
Due to the current lack of pointers, there are no strings. So printf
can't be used. Instead, you can use the putchar
function, which puts a character (as an int) on screen. Keep in mind that what you write only shows up once you put a newline character. ('\n'
, code point 10)
There are also built-in header files, which are used to declare functions that connect with Snap!. Built-in header files include <stdio.h>, <turtle.h>, and <sensing.h>.
<stdio.h>
This just declares the putchar function.
int putchar(int character)
<turtle.h>
This allows the use of pen (a.k.a. turtle graphics) to draw stuff.
int turtle_create(void)
creates a new turtle and returns its ID number. its ID will be used for every turtle function as the first parameter to identify which turtle to use
void turtle_remove(int id)
deletes a turtle with the given id. if any function is called with an invalid id, the function does nothing.
the rest of the functions are wrappers for pen/motion blocks:
void turtle_pendown(int id)
void turtle_penup(int id)
void turtle_penhsv(int id, int h, int s, int v)
void turtle_pensize(int id, int size)
void turtle_goto(int id, int x, int y)
void turtle_turn(int id, int degrees)
turns clockwise if positive, counter clockwise if negative
void turtle_move(int id, int steps)
<sensing.h>
These declare functions that allow interaction with the user's mouse and keyboard.
int mousex(void)
int mousey(void)
int mousedown(void)
returns 1 if mouse is down, else returns 0
int keydown(char key)
returns 1 if the key specified is pressed. e.g. 'a', '7'. however does not support space (because i'm lazy just use a different key) and arrow keys, because arrow keys in the sensing block need more than one character.
It isn't a very fast interpreter (cough because it's written in snap). But a plan I have is to make a separate project that, instead of interpreting the generated "abstract syntax tree" of the program directly, would compile it into JavaScript, then run the generated JavaScript code. The parser and the compiler would still be run on Snap! code. The reason I'm not doing that in the first place is because I'm pretty sure the devs are discouraging the use of JavaScript. So the user has to manually turn on JavaScript. Also there's no way to detect if JavaScript is enabled. (I guess since it throws an error if you try to run JS with it disabled, I could check if an error is thrown when JS is run. Except you need JS on to use the "Catch errors library"... Or maybe I could do something else... since code execution stops if there's an error...)
If you want to use this program but don't know basic C programming, then... uh... I don't know. Search it up online I guess. But the good thing is, the feature set of the current state of my C compiler is so small it's basically just strictly typed JavaScript. So if you know at least basic JavaScript (which I'm sure most regulars on this forum do), then lucky you! All you need to know is how types work in C.