Browse Source

Strings (?)

master
Yuki Izumi 4 years ago
parent
commit
1e3a275576
No known key found for this signature in database GPG Key ID: 44A3D2C95E26BB14
7 changed files with 55 additions and 17 deletions
  1. +3
    -3
      in.s
  2. +20
    -6
      lc/ast.c
  3. +7
    -3
      lc/ast.h
  4. +16
    -0
      lc/codegen.c
  5. +3
    -2
      lc/lexer.l
  6. +6
    -3
      lc/parser.y
  7. BIN
      out.o

+ 3
- 3
in.s View File

@@ -1,4 +1,4 @@
x = 1
y = 2
z = x + y + 3
x = "a"
y = "b"
z = x + y + "c"
z

+ 20
- 6
lc/ast.c View File

@@ -50,6 +50,9 @@ void expr_pp(FILE *f, expr const *e) {
case E_IMM_U8:
fprintf(f, "%02x", e->as.imm_u8);
break;
case E_IMM_STRING:
fprintf(f, "\"%s\"", e->as.imm_string);
break;
case E_REF:
fprintf(f, "%s", e->as.ref);
break;
@@ -71,6 +74,9 @@ void expr_free(expr *e) {
switch (e->type) {
case E_IMM_U8:
break;
case E_IMM_STRING:
free(e->as.imm_string);
break;
case E_REF:
free(e->as.ref);
break;
@@ -87,24 +93,32 @@ void expr_free(expr *e) {
}
}

expr *make_ref(char const *r) {
expr *make_ref(char *r) {
expr *e = calloc(1, sizeof(*e));
e->type = E_REF;
e->as.ref = strdup(r);
e->as.ref = r;
return e;
}

expr *make_number(char const *i) {
expr *make_number(int i) {
expr *e = calloc(1, sizeof(*e));
e->type = E_IMM_U8;
e->as.imm_u8 = atoi(i);
e->as.imm_u8 = i;
return e;
}

expr *make_string(char *s) {
expr *e = calloc(1, sizeof(*e));
e->type = E_IMM_STRING;
e->as.imm_string = strndup(s + 1, strlen(s) - 2);
free(s);
return e;
}

expr *make_assignment(char const *lhs, expr *rhs) {
expr *make_assignment(char *lhs, expr *rhs) {
expr *e = calloc(1, sizeof(*e));
e->type = E_ASSIGN;
e->as.assign.lhs = strdup(lhs);
e->as.assign.lhs = lhs;
e->as.assign.rhs = rhs;
return e;
}


+ 7
- 3
lc/ast.h View File

@@ -6,6 +6,7 @@

typedef enum expr_type {
E_IMM_U8,
E_IMM_STRING,
E_REF,
E_ASSIGN,
E_BINOP,
@@ -15,6 +16,7 @@ typedef struct expr {
expr_type type;
union {
uint8_t imm_u8;
char *imm_string;
char *ref;
struct {
char *lhs;
@@ -44,9 +46,11 @@ void ast_append_expr(ast *ast, expr *expr);

void expr_pp(FILE *f, expr const *e);
void expr_free(expr *e);
expr *make_ref(char const *r);
expr *make_number(char const *i);
expr *make_assignment(char const *lhs, expr *rhs);

expr *make_ref(char *r);
expr *make_number(int n);
expr *make_string(char *s);
expr *make_assignment(char *lhs, expr *rhs);
expr *make_binop(expr *lhs, char op, expr *rhs);

int yylex(void);


+ 16
- 0
lc/codegen.c View File

@@ -1,4 +1,5 @@
#include <stdlib.h>
#include <string.h>
#include "codegen.h"
#include "cfuhash.h"

@@ -12,6 +13,7 @@ enum {
STOu8,
RCu8,
DROP,
LDstr,
RET = 0xFF,
};

@@ -19,6 +21,16 @@ static void put8(FILE *f, uint8_t n) {
fputc(n, f);
}

static void put32(FILE *f, uint32_t n) {
n = htonl(n);
fwrite(&n, sizeof(n), 1, f);
}

static void putd(FILE *f, void *data, size_t len) {
put32(f, len);
fwrite(data, len, 1, f);
}

static cfuhash_table_t *vars;

void emit(FILE *f, expr const *e) {
@@ -27,6 +39,10 @@ void emit(FILE *f, expr const *e) {
put8(f, LDu8);
put8(f, e->as.imm_u8);
break;
case E_IMM_STRING:
put8(f, LDstr);
putd(f, e->as.imm_string, strlen(e->as.imm_string));
break;
case E_ASSIGN: {
emit(f, e->as.assign.rhs);
put8(f, STOu8);


+ 3
- 2
lc/lexer.l View File

@@ -5,8 +5,9 @@

%%

[0-9]+ { yylval.token = strdup(yytext); return NUMBER; }
[a-zA-Z]+ { yylval.token = strdup(yytext); return TOKEN; }
\"[^\"\n]*\" { yylval.str = strdup(yytext); return IMM_STRING; }
[0-9]+ { yylval.imm_number = atoi(yytext); return IMM_NUMBER; }
[a-zA-Z]+ { yylval.str = strdup(yytext); return TOKEN; }
[\t ]+ { }
. { return yytext[0]; }
\n { return NL; }


+ 6
- 3
lc/parser.y View File

@@ -7,14 +7,16 @@

%union {
expr *expr;
char *token;
char *str;
int imm_number;
}

%token END_OF_FILE 0 "$end"

%token NL

%token <token> TOKEN NUMBER
%token <str> TOKEN IMM_STRING
%token <imm_number> IMM_NUMBER

%type <expr> line expr

@@ -40,7 +42,8 @@ line_separator: NL
;

expr: TOKEN { $$ = make_ref($1); }
| NUMBER { $$ = make_number($1); }
| IMM_NUMBER { $$ = make_number($1); }
| IMM_STRING { $$ = make_string($1); }
| TOKEN '=' expr { $$ = make_assignment($1, $3); }
| expr '+' expr { $$ = make_binop($1, '+', $3); }
| expr '-' expr { $$ = make_binop($1, '-', $3); }


BIN
out.o View File


Loading…
Cancel
Save