#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <ctype.h>
#include "tokenise.h"
/*
============
Token stream
============
*/
void init_TokenStream( TokenStream* tokens, char* input ) {
//*tokens = input;
}
bool eqToken( Token a, Token b ) {
if (a.type!=b.type){
return false;
}
switch (a.type){
case T_FLOAT:
if (a.val.fltval==b.val.fltval){
return true;
}
break;
case T_INTEGER:
if (a.val.intval==b.val.intval){
return true;
}
default:
if (a.val.strval==b.val.strval){
return true;
}
}
}
Token current( TokenStream* tokens ) {
return **tokens;
}
void advance( TokenStream* tokens ) {
//(*tokens)++;
(*tokens)+=8;
}
bool end_of_tokens( TokenStream* tokens ) {
return current( tokens ).val.strval == '\0';
}
bool isVariable( Token tok ) {
// THIS FUNCTION SUCKS
// return isalpha( tok );
if (tok.type==T_IDENT){
return true;
}else{
return false;
}
}
bool isConstant( Token tok ) {
if (tok.type==T_INTEGER){
return tok.val.intval;
}
if (tok.type==T_FLOAT){
return tok.val.fltval;
}
//return isdigit( tok );
}
void print_token( Token tok ) {
if (tok.type==T_INTEGER){
printf( "%d", tok.val.intval );
}else if (tok.type==T_FLOAT){
printf( "%f", tok.val.fltval );
}else{
printf( "%s", tok.val.strval );
}
//printf( "%c", tok );
}
void expect( TokenStream* tokens, Token expected ) {
if( eqToken( current( tokens ), expected ) )
advance( tokens );
else {
error( "Expected '%c', but found '%c', near \"%.40s\"",
expected,
current( tokens ),
*tokens );
}
}
void init_predefined_tokens( ) {
TOK_ADD.type = T_SYMBOL;
TOK_ADD.val.strval = "+";
TOK_ASSIGN.type = T_SYMBOL;
TOK_ASSIGN.val.strval = "=";
TOK_CLOSEBRACE.type = T_SYMBOL;
TOK_CLOSEBRACE.val.strval = "}";
TOK_DIV.type = T_SYMBOL;
TOK_DIV.val.strval = "/";
TOK_DO.type = T_SYMBOL;
TOK_DO.val.strval = "d";
TOK_ELSE.type = T_SYMBOL;
TOK_ELSE.val.strval = "e";
TOK_EQ.type = T_SYMBOL;
TOK_EQ.val.strval = "=";
TOK_ERROR.type = T_SYMBOL;
TOK_ERROR.val.strval = "#";
TOK_IF.type = T_SYMBOL;
TOK_IF.val.strval = "i";
TOK_MUL.type = T_SYMBOL;
TOK_MUL.val.strval = "*";
TOK_NOT.type = T_SYMBOL;
TOK_NOT.val.strval = "!";
TOK_OPENBRACE.type = T_SYMBOL;
TOK_OPENBRACE.val.strval = "{";
TOK_PCLOSE.type = T_SYMBOL;
TOK_PCLOSE.val.strval = ")";
TOK_POPEN.type = T_SYMBOL;
TOK_POPEN.val.strval = "(";
TOK_PRINT.type = T_SYMBOL;
TOK_PRINT.val.strval = "p";
TOK_SEMICOLON.type = T_SYMBOL;
TOK_SEMICOLON.val.strval = ";";
TOK_SKIP.type = T_SYMBOL;
TOK_SKIP.val.strval = "k";
TOK_SUB.type = T_SYMBOL;
TOK_SUB.val.strval = "-";
TOK_THEN.type = T_SYMBOL;
TOK_THEN.val.strval = "t";
TOK_WHILE.type = T_SYMBOL;
TOK_WHILE.val.strval = "w";
}
void error( char* fmt, ... ) {
va_list args;
va_start( args, fmt );
vprintf( fmt, args );
va_end( args );
printf( "\n" );
}
void test_tokeniser( char* str ) {
TokenStream tokens;
init_TokenStream( &tokens, str );
for( ; ! end_of_tokens( &tokens ); advance( &tokens ) ) {
print_token( current( &tokens ) );
printf( "\n" );
}
}