• Importing Files

    Dylan Brophy10/29/2020 at 00:41 0 comments

    Well, it's reading files I import.  Which is good.  Now I just need to make them parse...

    And here's what was failing:

    class Lexer {
    	File file;
    	String buffer = "";
    	int line_number = 0;
    	Lexer(File file) { // line 10
    		this.file = file;
           .... more code ....

    It's the constructor.  I forgot to put a access restriction like 'public' or 'private' on it.  My parser must think it's manditory.  Debating weather it should be.

    One thing I don't care for in Java is that the default access has no keyword.  It's not *bad* but it seems like better code would be produced by requiring one.  Just thinking.

  • Printing compiler usage (from the interpreter)

    Dylan Brophy10/26/2020 at 15:20 0 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]");
    			println("Usage: " + argv[0] + " [filename]");
    	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 :)

  • Simple Python Interpreter and Plan

    Dylan Brophy10/26/2020 at 00:33 0 comments

    The interpreter can run simple programs but is basically a limited C interpreter for now

    void main(String[] args) {
    	println("Hello World!");
    	if (1 + 1 == 2) {
    		println("1 + 1 = 2!");

     This executes.  It's not super hard to get running because I had code from the last language I tried to make, written in Python 3 using rply.

    I want a compiler that can compile itself.  So my plan:

    1. Write a basic compiler in Kyazuken
    2. Get the interpreter to interpret it
    3. Try to compile the compiler with itself in the interpreter

    I want to output some intermediate language like RTL so I can generate machine code with GCC.  This way I could quickly generate compiled code for any processor.