// Usage: sqasm < file.in > file.out // Oleg Mazonka 10.11.2006 sqasm #include #include #include #include #include using namespace std; int str2int(const string &s) { int ret=0; sscanf(s.c_str(),"%d",&ret); return ret; } string int2str(int x, int pr=0) { char buf[40]; sprintf(buf,"%d",x); if( !pr ) return buf; string s = buf; while( s.size() < (unsigned)pr ) s = string("0")+s; return s; } /* grammar program := list of intructions intruction := [.] list of items ( ';' | '\n' ) item := [label:]expression label := id expression := ( term | term+expression | term-expression | -expression ) term := ( (expression) | const | id ) const := ( number | 'letter' | ? ) */ struct item { int addr; string s; int i; enum { EMPTY, STR, RES } state; item() : state(EMPTY) {} string dump(bool=false); }; string item::dump(bool extra) { string r; if( extra ) r = int2str(addr)+":"; if( state==RES ) return r+int2str(i); return r+ string("#")+s; } struct instruction { vector items; string dump(bool=false); }; string instruction::dump(bool extra) { string r; for( int i=0; i lab2adr; string prog; typedef string::size_type sint; sint pip = 0; int line=1; int addr = 0; map unres; void eat() { while( pip=prog.size() ) { cerr<<"Error "<=prog.size() ) return false; string lab; if( getlabel(lab) ) { if( lab2adr.find(lab) == lab2adr.end() ) lab2adr[lab] = addr; else cerr<<"Error "< program() { vector r; while( pip &pr) { for( int i=0; i pr = program(); for( int i=0; i::iterator i=unres.begin(); i!=unres.end(); i++ ) cerr<<" ["<first<<":"<second<<"]"; cerr<<'\n'; } }