SE250:lab-9:sdal039

From Marks Wiki
Revision as of 05:20, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (9 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

comiler... ok so we need a different function (perhaps) for each thing we want to do. Load idents, strings, perform operations, evaluate statements.

and now there's a whole heap of undefined references

cygdrive/c/Users/sdal039/AppData/Local/Temp/ccDd1mQd.o:parser.c:(.text+0x202): undefined reference  to `_end_of_tokens'
/cygdrive/c/Users/sdal039/AppData/Local/Temp/ccDd1mQd.o:parser.c:(.text+0x212): undefined reference to `_error'
/cygdrive/c/Users/sdal039/AppData/Local/Temp/ccDd1mQd.o:parser.c:(.text+0x240): undefined reference to `_current'
/cygdrive/c/Users/sdal039/AppData/Local/Temp/ccDd1mQd.o:parser.c:(.text+0x2a6): undefined reference to `_advance'

and so on...


aha.. toy tokeniser is up now. errors gone.


ok this is stupidly hard :P code so far:

static int strCount = 0;
void identifiers( Tree* pTree )
 {
      if (pTree != 0) {
	  if (isVariable( pTree->name )) {    //variable assignment
	      printf(".globl %c\n\t%c .long 0\n", pTree->name, pTree->name );
	  }

	  if (isConstant( pTree->name )) {   //string assignment
	      printf("S%d:\n\t.ascii \"%c\"\n", strCount, pTree->name);
	      strCount++;
	  }
     }

     int i;
     for (i = 0; i < pTree->arity ; i++)
	  identifiers( pTree->arg[i] );
}

Well it does do something.. given the token stream 'a=1' it outputs

.globl a
	a .long 0
S0:
	.ascii "1"


adding two numbers...

Tree* compile(Tree* t) 
{
   if (t != 0) {
	if (eqToken( t->name, TOK_ADD )) {//addition
		Tree* left = compile( t->arg[0] );//get first opperand
		Tree* right = compile( t->arg[1] );//get second opperand
		printf("pop\%%c\npop\%%c\nadd \%%c, \%%c\npush\%%c", left->name, right->name,  right->name, left->name, left->name);
	    }
   }
	return t;
}

segmentation fault...yaaaayyyy. >_<


What makes this task hard is that without proper implementations of the tokeniser and parse tree it's really hard to write. toy-tokeniser doesn't really allow you to test things, similarly with the parse tree. for example, it can't easily differentiate between a variable 'i' and and if statement as it tokenises ifs to just 'i'. Can't represent quoted strings either as each token is simply 1 character.

Mark suggested doing these three tasks (or at least the first two) over two/three separate labs, in the order than they are used. this would make it easier at each stage and at the end we'd have a pretty solid parser with everyone working on one bit at a time.