Skip to content

Commit

Permalink
Token types
Browse files Browse the repository at this point in the history
  • Loading branch information
ogamespec committed Sep 9, 2023
1 parent 3bf5c0d commit 82d265b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
1 change: 0 additions & 1 deletion Tools/Breakasm/asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ struct define_s {
#define EVAL_ADDRESS 2 // $aabb
#define EVAL_LABEL 3 // BEGIN + including complex expressions that are saved as Label, but the expression is evaluated on the second pass
#define EVAL_STRING 4 // "Hello", 'Hello'
#define EVAL_OP 5 // Operation (+, -, etc.), is used only in the eval_expr method

struct eval_t {
int type;
Expand Down
36 changes: 18 additions & 18 deletions Tools/Breakasm/asmexpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void put_back_char(char** pp)
static token_t* create_op_token(OPS optype)
{
token_t* token = new token_t;
token->type = EVAL_OP;
token->type = TOKEN_OP;
token->op = optype;
return token;
}
Expand Down Expand Up @@ -65,7 +65,7 @@ static token_t* next_token(char** pp)
*ptr++ = 0;
//printf("number: %s\n", buf);
token = new token_t;
token->type = EVAL_NUMBER;
token->type = TOKEN_NUMBER;
token->number = strtoul(buf, nullptr, base);
return token;
}
Expand All @@ -85,7 +85,7 @@ static token_t* next_token(char** pp)
*ptr++ = 0;
//printf("ident: %s\n", buf);
token = new token_t;
token->type = EVAL_LABEL;
token->type = TOKEN_IDENT;
strcpy(token->string, buf);
return token;
}
Expand Down Expand Up @@ -118,7 +118,7 @@ static token_t* next_token(char** pp)
*ptr++ = 0;
printf("string: %s\n", buf);
token = new token_t;
token->type = EVAL_STRING;
token->type = TOKEN_STRING;
strcpy(token->string, buf);
return token;
}
Expand Down Expand Up @@ -277,17 +277,17 @@ static void dump_tokens(std::list<token_t*>& tokens)
token_t* token = *it;
switch (token->type)
{
case EVAL_NUMBER:
printf("EVAL_NUMBER: %d (0x%08X)\n", token->number, token->number);
case TOKEN_NUMBER:
printf("TOKEN_NUMBER: %d (0x%08X)\n", token->number, token->number);
break;
case EVAL_LABEL:
printf("EVAL_IDENT: %s\n", token->string);
case TOKEN_IDENT:
printf("TOKEN_IDENT: %s\n", token->string);
break;
case EVAL_STRING:
printf("EVAL_STRING: %s\n", token->string);
case TOKEN_STRING:
printf("TOKEN_STRING: %s\n", token->string);
break;
case EVAL_OP:
printf("EVAL_OP: %s\n", opstr(token->op));
case TOKEN_OP:
printf("TOKEN_OP: %s\n", opstr(token->op));
break;
}
}
Expand Down Expand Up @@ -364,7 +364,7 @@ static node_t * evaluate (std::list<node_t*>& tree, node_t * expr, long *lvalue)

// optional unary operation
token = curr->token;
if ( token->type == EVAL_OP && isunary(token->op) ) {
if ( token->type == TOKEN_OP && isunary(token->op) ) {
if (curr->rvalue == NULL) {
printf("Missing identifier\n");
errors++;
Expand All @@ -380,7 +380,7 @@ static node_t * evaluate (std::list<node_t*>& tree, node_t * expr, long *lvalue)

// mandatory identifier or nested expression
token = curr->token;
if ( token->type == EVAL_LABEL || token->type == EVAL_NUMBER || curr->depth > expr->depth ) {
if ( token->type == TOKEN_IDENT || token->type == TOKEN_NUMBER || curr->depth > expr->depth ) {

mvalue = 0;

Expand All @@ -389,7 +389,7 @@ static node_t * evaluate (std::list<node_t*>& tree, node_t * expr, long *lvalue)
curr = evaluate (tree, curr, &mvalue);
//printf ( "SUB LVALUE : %i\n", mvalue.num.value );
}
else if ( token->type == EVAL_LABEL) {
else if ( token->type == TOKEN_IDENT) {
curr = curr->rvalue;

define_s* def = define_lookup(token->string);
Expand All @@ -407,7 +407,7 @@ static node_t * evaluate (std::list<node_t*>& tree, node_t * expr, long *lvalue)
}
if (debug) printf ( "SYMBOL: %s", token->string );
}
else if ( token->type == EVAL_NUMBER) {
else if ( token->type == TOKEN_NUMBER) {
curr = curr->rvalue;
mvalue = token->number;
if (debug) printf ( "NUMBER(%i) ", mvalue );
Expand Down Expand Up @@ -457,7 +457,7 @@ static node_t * evaluate (std::list<node_t*>& tree, node_t * expr, long *lvalue)
// optional binary operation
if (curr && curr->depth == expr->depth ) {
token = curr->token;
if ( token->type == EVAL_OP && isbinary(token->op) ) {
if ( token->type == TOKEN_OP && isbinary(token->op) ) {
curr = curr->rvalue;
op = token->op;
}
Expand Down Expand Up @@ -487,7 +487,7 @@ static void grow (tree_t& tree, node_t **expr, token_t * token)
tree.initialized = 1;
}

if ( token->type == EVAL_OP) {
if ( token->type == TOKEN_OP) {
if ( token->op == OPS::LPAREN) {
tree.prio++;
tree.depth++;
Expand Down
11 changes: 8 additions & 3 deletions Tools/Breakasm/asmexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

#pragma once

#define TOKEN_NUMBER 1 // #$12, $aabb
#define TOKEN_IDENT 2 // identifier (label / define)
#define TOKEN_STRING 3 // "Hello", 'Hello'
#define TOKEN_OP 4 // Operation (+, -, etc.)

/// <summary>
/// Operations for composite expressions. Used in the eval_expr method.
/// </summary>
Expand All @@ -24,10 +29,10 @@ enum class OPS
};

struct token_t {
int type; // Token type, overused definition EVAL_xxx; EVAL_LABEL in this case means that the token is an identifier
int type; // Token type
OPS op; // Type of operation (see OPS)
long number; // Converted number (EVAL_NUMBER)
char string[0x100]; // String representation for EVAL_LABEL and EVAL_STRING
long number; // Converted number (TOKEN_NUMBER)
char string[0x100]; // String representation for TOKEN_IDENT and TOKEN_STRING
};

struct node_t
Expand Down

0 comments on commit 82d265b

Please sign in to comment.