Skip to content

Commit 64b5b9f

Browse files
committed
Add support for 'strcmp'.
1 parent 2932a9a commit 64b5b9f

File tree

7 files changed

+1117
-1051
lines changed

7 files changed

+1117
-1051
lines changed

lexical.c

Lines changed: 261 additions & 252 deletions
Large diffs are not rendered by default.

lexical.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ extern int yylex \
714714
#undef yyTABLES_NAME
715715
#endif
716716

717-
#line 458 "lexical.l"
717+
#line 459 "lexical.l"
718718

719719

720720
#line 720 "lexical.h"

lexical.l

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ pc return TOKEN_PROGRAM_COUNTER;
307307

308308
/* Functions. */
309309
strlen return TOKEN_STRLEN;
310+
strcmp return TOKEN_STRCMP;
310311
def return TOKEN_DEF;
311312

312313
/* Decimal number. */

semantic.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ static cc_bool ResolveExpression(SemanticState *state, Expression *expression, u
774774
case EXPRESSION_PROGRAM_COUNTER_OF_STATEMENT:
775775
case EXPRESSION_PROGRAM_COUNTER_OF_EXPRESSION:
776776
case EXPRESSION_STRLEN:
777+
case EXPRESSION_STRCMP:
777778
case EXPRESSION_DEF:
778779
case EXPRESSION_NEGATE:
779780
case EXPRESSION_BITWISE_NOT:
@@ -878,6 +879,7 @@ static cc_bool ResolveExpression(SemanticState *state, Expression *expression, u
878879
case EXPRESSION_PROGRAM_COUNTER_OF_STATEMENT:
879880
case EXPRESSION_PROGRAM_COUNTER_OF_EXPRESSION:
880881
case EXPRESSION_STRLEN:
882+
case EXPRESSION_STRCMP:
881883
case EXPRESSION_DEF:
882884
case EXPRESSION_SUBTRACT:
883885
case EXPRESSION_ADD:
@@ -966,6 +968,10 @@ static cc_bool ResolveExpression(SemanticState *state, Expression *expression, u
966968
*value = strlen(expression->shared.string);
967969
break;
968970

971+
case EXPRESSION_STRCMP:
972+
*value = strcmp(expression->shared.subexpressions[0].shared.string, expression->shared.subexpressions[1].shared.string) == 0;
973+
break;
974+
969975
case EXPRESSION_DEF:
970976
*value = LookupSymbol(state, expression->shared.string, strlen(expression->shared.string)) != NULL;
971977
break;
@@ -998,6 +1004,7 @@ static cc_bool ResolveExpression(SemanticState *state, Expression *expression, u
9981004
case EXPRESSION_NEGATE:
9991005
case EXPRESSION_BITWISE_NOT:
10001006
case EXPRESSION_LOGICAL_NOT:
1007+
case EXPRESSION_STRCMP:
10011008
free(expression->shared.subexpressions);
10021009
break;
10031010

syntactic.c

Lines changed: 820 additions & 787 deletions
Large diffs are not rendered by default.

syntactic.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ typedef enum ExpressionType
250250
EXPRESSION_PROGRAM_COUNTER_OF_STATEMENT,
251251
EXPRESSION_PROGRAM_COUNTER_OF_EXPRESSION,
252252
EXPRESSION_STRLEN,
253+
EXPRESSION_STRCMP,
253254
EXPRESSION_DEF
254255
} ExpressionType;
255256

@@ -431,7 +432,7 @@ typedef struct Statement
431432
} Statement;
432433

433434

434-
#line 435 "syntactic.h"
435+
#line 436 "syntactic.h"
435436

436437
/* Token kinds. */
437438
#ifndef M68KASM_TOKENTYPE
@@ -610,7 +611,8 @@ typedef struct Statement
610611
TOKEN_LEFT_SHIFT = 423, /* TOKEN_LEFT_SHIFT */
611612
TOKEN_RIGHT_SHIFT = 424, /* TOKEN_RIGHT_SHIFT */
612613
TOKEN_STRLEN = 425, /* TOKEN_STRLEN */
613-
TOKEN_DEF = 426 /* TOKEN_DEF */
614+
TOKEN_STRCMP = 426, /* TOKEN_STRCMP */
615+
TOKEN_DEF = 427 /* TOKEN_DEF */
614616
};
615617
typedef enum m68kasm_tokentype m68kasm_token_kind_t;
616618
#endif
@@ -619,7 +621,7 @@ typedef struct Statement
619621
#if ! defined M68KASM_STYPE && ! defined M68KASM_STYPE_IS_DECLARED
620622
union M68KASM_STYPE
621623
{
622-
#line 434 "syntactic.y"
624+
#line 435 "syntactic.y"
623625

624626
unsigned long unsigned_long;
625627
char *string;
@@ -632,7 +634,7 @@ union M68KASM_STYPE
632634
IdentifierList identifier_list;
633635
Expression expression;
634636

635-
#line 636 "syntactic.h"
637+
#line 638 "syntactic.h"
636638

637639
};
638640
typedef union M68KASM_STYPE M68KASM_STYPE;
@@ -646,13 +648,13 @@ typedef union M68KASM_STYPE M68KASM_STYPE;
646648
int m68kasm_parse (void *scanner, Statement *statement);
647649

648650
/* "%code provides" blocks. */
649-
#line 408 "syntactic.y"
651+
#line 409 "syntactic.y"
650652

651653

652654
void DestroyExpression(Expression *expression);
653655
void DestroyStatement(Statement *statement);
654656

655657

656-
#line 657 "syntactic.h"
658+
#line 659 "syntactic.h"
657659

658660
#endif /* !YY_M68KASM_SYNTACTIC_H_INCLUDED */

syntactic.y

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ typedef enum ExpressionType
223223
EXPRESSION_PROGRAM_COUNTER_OF_STATEMENT,
224224
EXPRESSION_PROGRAM_COUNTER_OF_EXPRESSION,
225225
EXPRESSION_STRLEN,
226+
EXPRESSION_STRCMP,
226227
EXPRESSION_DEF
227228
} ExpressionType;
228229

@@ -612,6 +613,7 @@ static void DestroyStatementInstruction(StatementInstruction *instruction);
612613
%token TOKEN_LEFT_SHIFT
613614
%token TOKEN_RIGHT_SHIFT
614615
%token TOKEN_STRLEN
616+
%token TOKEN_STRCMP
615617
%token TOKEN_DEF
616618

617619
%type<instruction> instruction
@@ -624,13 +626,13 @@ static void DestroyStatementInstruction(StatementInstruction *instruction);
624626
%type<unsigned_long> data_or_address_register
625627
%type<expression_list> expression_list
626628
%type<identifier_list> identifier_list
627-
%type<expression> expression expression1 expression2 expression3 expression4 expression5 expression6 expression7 expression8
629+
%type<expression> expression expression1 expression2 expression3 expression4 expression5 expression6 expression7 expression8 string
628630

629631
%destructor { free($$); } TOKEN_IDENTIFIER TOKEN_LOCAL_IDENTIFIER TOKEN_STRING
630632
%destructor { DestroyOperand(&$$); } operand
631633
%destructor { DestroyExpressionList(&$$); } expression_list
632634
%destructor { DestroyIdentifierList(&$$); } identifier_list
633-
%destructor { DestroyExpression(&$$); } expression expression1 expression2 expression3 expression4 expression5 expression6 expression7 expression8
635+
%destructor { DestroyExpression(&$$); } expression expression1 expression2 expression3 expression4 expression5 expression6 expression7 expression8 string
634636

635637
%start statement
636638

@@ -1971,10 +1973,9 @@ expression8
19711973
free($2);
19721974
}
19731975
}
1974-
| TOKEN_STRING
1976+
| string
19751977
{
1976-
$$.type = EXPRESSION_STRING;
1977-
$$.shared.string = $1;
1978+
$$ = $1;
19781979
}
19791980
| '*'
19801981
{
@@ -1993,13 +1994,25 @@ expression8
19931994
$$.type = EXPRESSION_STRLEN;
19941995
$$.shared.string = $3;
19951996
}
1997+
| TOKEN_STRCMP '(' string ',' string ')'
1998+
{
1999+
if (!DoExpression(&$$, EXPRESSION_STRLEN, &$3, &$5))
2000+
YYNOMEM;
2001+
}
19962002
| TOKEN_DEF '(' TOKEN_IDENTIFIER ')'
19972003
{
19982004
$$.type = EXPRESSION_DEF;
19992005
$$.shared.string = $3;
20002006
}
20012007
;
20022008

2009+
string
2010+
: TOKEN_STRING
2011+
{
2012+
$$.type = EXPRESSION_STRING;
2013+
$$.shared.string = $1;
2014+
}
2015+
20032016
%%
20042017

20052018
static cc_bool DoExpression(Expression *expression, ExpressionType type, Expression *left_expression, Expression *right_expression)
@@ -2052,6 +2065,7 @@ void DestroyExpression(Expression *expression)
20522065
case EXPRESSION_MORE_OR_EQUAL:
20532066
case EXPRESSION_LEFT_SHIFT:
20542067
case EXPRESSION_RIGHT_SHIFT:
2068+
case EXPRESSION_STRCMP:
20552069
DestroyExpression(&expression->shared.subexpressions[0]);
20562070
DestroyExpression(&expression->shared.subexpressions[1]);
20572071
free(expression->shared.subexpressions);

0 commit comments

Comments
 (0)