Skip to content

Commit f414999

Browse files
committed
Implement println (instead of print).
1 parent 36d9c93 commit f414999

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

AST.c

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ AST* AST_makeValue(int v) {
3636
return p;
3737
}
3838

39+
AST *AST_makeString(const char *str) {
40+
AST *p = AST_alloc();
41+
p->code = VAL_STRING;
42+
p->AST_string = str;
43+
return p;
44+
}
45+
3946
AST *AST_makeUnary(CodeType code, AST *node) {
4047
AST *p = AST_alloc();
4148
p->code = code;

AST.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ enum code_ {
1818
ETC_LIST,
1919
VAL_NUM,
2020
VAL_SYMBOL,
21-
CODE_PRINT,
21+
VAL_STRING,
22+
CODE_PRINTLN,
2223
CODE_FUNC,
2324
CODE_VAR,
2425
CODE_RETURN,
@@ -74,6 +75,9 @@ struct AST_ {
7475
/* For value (leaf node) */
7576
int value;
7677

78+
/* For string */
79+
const char *str;
80+
7781
/* For symbol */
7882
Symbol *symbol;
7983

@@ -96,6 +100,7 @@ struct AST_ {
96100
};
97101

98102
#define AST_value un.value
103+
#define AST_string un.str
99104
/* For Unary Expression */
100105
#define AST_unary un.unary
101106
/* For Binary Expression */
@@ -112,6 +117,7 @@ struct AST_ {
112117

113118
/* Normal Expression */
114119
AST *AST_makeValue(int v);
120+
AST *AST_makeString(const char *str);
115121
AST *AST_makeUnary(CodeType type, AST *node);
116122
AST *AST_makeBinary(CodeType type, AST *left, AST *right);
117123
AST *AST_makeTrinary(CodeType type, AST *first, AST *second, AST *third);

interpreter.c

+12-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int executeArrayAssign(AST *ast, AST *idx, AST *expr);
3939
int resolveSymbol(const Symbol *symbol, const size_t index);
4040

4141
int main() {
42-
yydebug = 0;
42+
yydebug = 1;
4343
SymbolTable = StrSymMap_new();
4444
Env = Stack_new();
4545
printf("*** start parsing ***\n");
@@ -201,8 +201,15 @@ int executeArrayAssign(AST *sym, AST *idx, AST *expr) {
201201
return arrayAssign(sym->AST_symbol, executeExpression(idx), executeExpression(expr));
202202
}
203203

204-
void executePrint(AST *ast) {
205-
printf("%d\n", executeExpression(ast->AST_unary));
204+
void executePrintln(AST *ast) {
205+
const char *format = ast->AST_left->AST_string;
206+
if (ast->AST_right) {
207+
const int val = executeExpression(ast->AST_right);
208+
printf(format, val);
209+
} else {
210+
printf(format);
211+
}
212+
printf("\n");
206213
}
207214

208215
int executeStatements(ASTVector *statements) {
@@ -232,8 +239,8 @@ bool executeStatement(AST *ast) {
232239
case ETC_LIST:
233240
executeStatements(ast->AST_list);
234241
break;
235-
case CODE_PRINT:
236-
executePrint(ast);
242+
case CODE_PRINTLN:
243+
executePrintln(ast);
237244
break;
238245
case CODE_RETURN:
239246
if (ast->AST_unary) {

lexer.c

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <ctype.h>
44
#include "AST.h"
55

6+
#define INITIAL_STR_LEN (128)
7+
68
int take() {
79
int c;
810
while (isspace(c = getc(stdin))) { }
@@ -26,6 +28,7 @@ int yylex() {
2628
case '[':
2729
case ']':
2830
case ';':
31+
case ',':
2932
return c;
3033
}
3134
if (c == '=') {
@@ -60,6 +63,18 @@ int yylex() {
6063
ungetc(next, stdin);
6164
return c;
6265
}
66+
if (c == '"') {
67+
char *str = (char *)malloc(sizeof(char) * INITIAL_STR_LEN);
68+
char *s = str;
69+
c = getc(stdin); // skip current '"'
70+
for (; c != '"'; c = getc(stdin), ++s) {
71+
*s = c;
72+
}
73+
*s = '\0';
74+
yylval.val = AST_makeString(strdup(str));
75+
free(str);
76+
return STRING;
77+
}
6378
if (isdigit(c)) {
6479
int n;
6580
for (n = 0; isdigit(c); c = getc(stdin)) {
@@ -78,8 +93,8 @@ int yylex() {
7893
*s = '\0';
7994
ungetc(c, stdin);
8095
int retval = 0;
81-
if (strcmp(str, "print") == 0) {
82-
retval = PRINT;
96+
if (strcmp(str, "println") == 0) {
97+
retval = PRINTLN;
8398
} else if (strcmp(str, "var") == 0) {
8499
retval = VAR;
85100
} else if (strcmp(str, "return") == 0) {

parser.y

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
%token PRINT
1+
%token PRINTLN
22
%token VAR
33
%token RETURN
44
%token FOR
55
%token SYMBOL
66
%token NUMBER
7+
%token STRING
78
%token OP_EQ
89
%token OP_NEQ
910
%token OP_LE
@@ -37,7 +38,7 @@
3738
%type <val> function_definition param_list symbol_list
3839
%type <val> variable_names
3940
%type <val> argument_list arguments
40-
%type <val> SYMBOL NUMBER
41+
%type <val> SYMBOL NUMBER STRING
4142

4243
%start program
4344

@@ -86,7 +87,9 @@ statement: expression ';'
8687
| RETURN ';' { $$ = AST_makeUnary(CODE_RETURN, NULL); }
8788
| for_statement
8889

89-
print_statement: PRINT expression { $$ = AST_makeUnary(CODE_PRINT, $2); }
90+
print_statement: PRINTLN '(' STRING ')' { $$ = AST_makeBinary(CODE_PRINTLN, $3, NULL); }
91+
| PRINTLN '(' STRING ',' expression ')'
92+
{ $$ = AST_makeBinary(CODE_PRINTLN, $3, $5); }
9093

9194
for_statement: FOR '(' expression ';' expression ';' expression ')' stmt_or_block
9295
{ $$ = AST_makeFor($3, $5, $7, $9); }

0 commit comments

Comments
 (0)