Close

Printing compiler usage (from the interpreter)

A project log for Kyazuken: a Java-like language

Java is great. Could I roll my own?

Dylan BrophyDylan Brophy 10/26/2020 at 15:200 Comments

I just got the manin() method of the compiler to run.  Mostly - the exit() function is not implemented yet :P

I have basically just been fixing anything I find that breaks every time I run the interpreter since it's so easy to execute.

Although printing the usage seems simple, making an interpreter to properly handle my main method is more complex than may be expected.  Here is the function that ran:

void main(String[] argv) {
	if (argv.length() != 2) {
		if (argv.length() == 0)
			println("Warning: problem detected in execution environment: argv.length() is 0.\n Usage: kyac [filename]");
		else
			println("Usage: " + argv[0] + " [filename]");
		exit(100);
	}

	println("Needed files will automatically be added.");
}

The first way that it crashed at first was with the argv.length().  Here you have a member function call on an object with a built-in type.  You need to make the interpreter understand the difference between a member function call and a regular function call.  It also needs to interface objects, types, and their functions in Kyazuken to their equivelents in Python.  Then I made this dumb mistake where I forgot to add the '!=' operator XD

Next I was badly parsing if blocks without curly braces, and I wasn't parsing else blocks at all.  The curly braces were sort of interesting.  Code inside curly braces, inside a function, are essentially just a single statement that contains multiple statements.

If we think of it like that, an if block is really just this:

if (condition) statement

Since a statement could be either a line of code ending in a semicolon, or a block of statements enclosed in curly braces, the only difference between an if block with curly braces and one without is the statement.  The structure of the if block is exactly the same.  The same can be said for while and for blocks.

Here is my Python implementation of these blocks (so far):

@self.pg.production('statement : IF OPEN_PAREN expression CLOSE_PAREN statement ELSE statement')
def if_else_block(p):
    x = IfElseBlock(p[2], p[4], p[6])
    x.lineinfo = p[0].getsourcepos()
    return x

@self.pg.production('statement : IF OPEN_PAREN expression CLOSE_PAREN statement')
def if_block(p):
    x = IfBlock(p[2], p[4])
    x.lineinfo = p[0].getsourcepos()
    return x

@self.pg.production('statement : FOR OPEN_PAREN var_dec COLON expression CLOSE_PAREN statement')
def iterator_for_block(p):
    x = IterForBlock(p[2], p[4], p[6])
    x.lineinfo = p[0].getsourcepos()
    return x

@self.pg.production('statement : WHILE OPEN_PAREN expression CLOSE_PAREN statement')
def while_loop(p):
    x = WhileLoop(p[2], p[4])
    x.lineinfo = p[0].getsourcepos()
    return x

@self.pg.production('statement : OPEN_CURLY statement_li CLOSE_CURLY')
def statement_group(p):
    return StatementList(p[1])

I still need to add the familiar C style 'for(;;)' syntax.

Surprisingly I had no issues with string concatenation.  Thanks Python.  Will probably bite me later.

If you read this far, thanks!  I hope you enjoy and keep updated :)

Discussions