Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support string literal #21

Merged
merged 4 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output: main.o lex.o grammar.o create.o compiler.o codegen.o opcode.o
output: main.o lex.o grammar.o create.o string.o compiler.o codegen.o opcode.o
$(CC) -o bin/main $^
main.o : main/main.c
$(CC) -c main/main.c
Expand All @@ -16,9 +16,11 @@ grammar.o : compiler/grammar.y
lex.o : compiler/lex.l grammar.o
lex $<
$(CC) -c lex.yy.c -o lex.o
string.o : compiler/string.c
$(CC) -c $< -o string.o

clean:
rm y.tab.*
rm lex.yy.c
rm *.o
rm bin/main
rm -f y.tab.*
rm -f lex.yy.c
rm -f *.o
rm -f bin/main
7 changes: 7 additions & 0 deletions compiler/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ Expression *alloc_bool_expression(bool value)
return expr;
}

Expression *alloc_string_expression(char *value)
{
Expression *expr = alloc_expression(STRING_EXPRESSION);
expr->u.str_value = value;
return expr;
}

Expression *alloc_unary_expression(ExpressionKind kind, Expression *unaryExpr)
{
Expression *expr = alloc_expression(kind);
Expand Down
8 changes: 5 additions & 3 deletions compiler/grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ int yyerror(const char *s);

%union {
char *identifier;
char *str_value;
double double_value;
int int_value;
struct Expression* expression;
Expand All @@ -24,10 +25,10 @@ int yyerror(const char *s);
struct ArgumentList *argument_list;
}

%token <int_value> BOOL_LITERAL
%token <double_value> DOUBLE_LITERAL
%token <int_value> INT_LITERAL
%token <identifier> IDENTIFIER;
%token <int_value> INT_LITERAL BOOL_LITERAL
%token <str_value> STRING_LITERAL
%token <identifier> IDENTIFIER
%token VAR CONST FUNCTION IF ELSE FOR RETURN BREAK CONTINUE NIL
%token BOOL_T INT_T DOUBLE_T STRING_T

Expand Down Expand Up @@ -183,6 +184,7 @@ literal:
INT_LITERAL { $$ = alloc_int_expression($1); }
| DOUBLE_LITERAL { $$ = alloc_double_expression($1); }
| BOOL_LITERAL { $$ = alloc_bool_expression($1); }
| STRING_LITERAL { $$ = alloc_string_expression($1); }
;

bool_expr:
Expand Down
49 changes: 44 additions & 5 deletions compiler/lex.l
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,57 @@
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
#include "compiler/summoner.h"

void lex_err(char *str) {
fprintf(stderr, "lexical error:%s\n", str);
exit(1);
}
%}

%x COMMENT
%x COMMENT STRING_STATE STRING_ESCAPE RAW_STRING_STATE
%%

"//" BEGIN(COMMENT);
<COMMENT>\n { BEGIN(0); return '\n'; }
<COMMENT>. ; // eat anything in comment

` {
open_string_literal();
BEGIN(RAW_STRING_STATE);
}
<RAW_STRING_STATE>` {
yylval.str_value = close_string_literal();
BEGIN(0);
return STRING_LITERAL;
}
<RAW_STRING_STATE>\n add_string_literal('\n');
<RAW_STRING_STATE>. add_string_literal(yytext[0]);

\" {
open_string_literal();
BEGIN(STRING_STATE);
}

<STRING_STATE>\" {
yylval.str_value = close_string_literal();
BEGIN(0);
return STRING_LITERAL;
}
<STRING_STATE>\\ BEGIN(STRING_ESCAPE);
<STRING_STATE>\n lex_err("new line in string");
<STRING_STATE>. add_string_literal(yytext[0]);

<STRING_ESCAPE>\" { add_string_literal('"'); BEGIN(STRING_STATE); }
<STRING_ESCAPE>b { add_string_literal('\b'); BEGIN(STRING_STATE); }
<STRING_ESCAPE>f { add_string_literal('\f'); BEGIN(STRING_STATE); }
<STRING_ESCAPE>n { add_string_literal('\n'); BEGIN(STRING_STATE); }
<STRING_ESCAPE>r { add_string_literal('\r'); BEGIN(STRING_STATE); }
<STRING_ESCAPE>t { add_string_literal('\t'); BEGIN(STRING_STATE); }
<STRING_ESCAPE>v { add_string_literal('\v'); BEGIN(STRING_STATE); }
<STRING_ESCAPE>\\ { add_string_literal('\\'); BEGIN(STRING_STATE); }
<STRING_ESCAPE>. lex_err("unknow escape");

[+\-*/\(\)<>!{}\n=,] {
return *yytext;
}
Expand Down Expand Up @@ -65,10 +107,7 @@

[ \t] ;

. {
fprintf(stderr, "lexical error:%s\n", yytext);
exit(1);
}
. { lex_err(yytext); }
%%

int yywrap(void) {
Expand Down
44 changes: 44 additions & 0 deletions compiler/string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int STRING_ALLOC_SIZE = 128;

static char *st_string_literal_buffer = NULL;
static int st_string_literal_buffer_size = 0;
static int st_string_literal_buffer_alloc_size = 0;

void open_string_literal(void)
{
st_string_literal_buffer_size = 0;
}

void add_string_literal(int letter)
{
if (st_string_literal_buffer_size == st_string_literal_buffer_alloc_size)
{
st_string_literal_buffer_alloc_size += STRING_ALLOC_SIZE;
st_string_literal_buffer = (char *) realloc(st_string_literal_buffer,
st_string_literal_buffer_alloc_size);
}
st_string_literal_buffer[st_string_literal_buffer_size] = letter;
st_string_literal_buffer_size++;
}

void reset_string_literal_buffer()
{
free(st_string_literal_buffer);
st_string_literal_buffer = NULL;
st_string_literal_buffer_size = 0;
st_string_literal_buffer_alloc_size = 0;
}

char *close_string_literal(void)
{
char *p = (char *) malloc(st_string_literal_buffer_size + 1);
strcpy(p, st_string_literal_buffer);
p[st_string_literal_buffer_size] = '\0';
reset_string_literal_buffer();
return p;
}
8 changes: 8 additions & 0 deletions compiler/summoner.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ typedef enum
BOOL_EXPRESSION = 1,
INT_EXPRESSION,
DOUBLE_EXPRESSION,
STRING_EXPRESSION,
IDENTIFIER_EXPRESSION,
FUNC_CALL_EXPRESSION,
ADD_EXPRESSION,
Expand Down Expand Up @@ -53,6 +54,7 @@ typedef struct Expression
bool boolean_value;
int int_value;
double double_value;
char *str_value;
char *identifier;
struct BinaryExpression *binary_expression;
struct Expression *unary_expression;
Expand Down Expand Up @@ -83,6 +85,7 @@ Expression *alloc_expression(ExpressionKind kind);
Expression *alloc_int_expression(int value);
Expression *alloc_double_expression(double value);
Expression *alloc_bool_expression(bool value);
Expression *alloc_string_expression(char *value);
Expression *alloc_identifier_expression(char *identifier);
Expression *alloc_unary_expression(ExpressionKind kind, Expression *unaryExpr);
Expression *alloc_binary_expression(ExpressionKind kind, Expression *left, Expression *right);
Expand Down Expand Up @@ -309,4 +312,9 @@ typedef struct SVM_Executable
SVM_CodeBlock top_level;
} SVM_Executable;

/** string.c */
void open_string_literal(void);
void add_string_literal(int letter);
char *close_string_literal(void);

#endif
7 changes: 7 additions & 0 deletions interpreter/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ ExprValue Interpreter::eval_expression(Expression *expr)
v.type = EXPR_DOUBLE_VALUE;
v.u.double_value = expr->u.double_value;
return v;
case STRING_EXPRESSION:
v.type = EXPR_STRING_VALUE;
v.u.str_value = expr->u.str_value;
return v;
case MINUS_EXPRESSION:
v = this->eval_expression(expr->u.unary_expression);
if (v.type == EXPR_INT_VALUE)
Expand Down Expand Up @@ -409,6 +413,9 @@ void print_expr_value(ExprValue val)
case EXPR_BOOL_VALUE:
printf(">>>%s\n", val.u.boolean_value ? "true" : "false");
break;
case EXPR_STRING_VALUE:
printf(">>>%s\n", val.u.str_value);
break;
default:
printf("invalid expression type when print expr value:%d", val.type);
exit(1);
Expand Down
2 changes: 2 additions & 0 deletions interpreter/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef enum
EXPR_BOOL_VALUE = 1,
EXPR_INT_VALUE,
EXPR_DOUBLE_VALUE,
EXPR_STRING_VALUE,
} ExprValueType;

typedef struct ExprValue
Expand All @@ -23,6 +24,7 @@ typedef struct ExprValue
bool boolean_value;
int int_value;
double double_value;
char *str_value;
} u;

} ExprValue;
Expand Down