Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ unary = deref identifier
| "(" expr ")"
| identifier
| integer
| char_literal
char_literal = "'" <character> "'"
op = "+" | "-" | "&" | "|" | "^" | "<<" | ">>"
| "==" | "!=" | "<" | ">" | "<=" | ">="
```
Expand Down
36 changes: 36 additions & 0 deletions examples/test_char_literals.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
int ch;

void test_char_literals()
{
ch = 'H';
print_char();
ch = 'e';
print_char();
ch = 'l';
print_char();
ch = 'l';
print_char();
ch = 'o';
print_char();
ch = ' ';
print_char();
ch = 'W';
print_char();
ch = 'o';
print_char();
ch = 'r';
print_char();
ch = 'l';
print_char();
ch = 'd';
print_char();
ch = '!';
print_char();
print_newline();
}

void _start()
{
test_char_literals();
while( 1 ){ }
}
15 changes: 15 additions & 0 deletions lint/lint.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,21 @@ static void tok_next(void)
continue;
}

// Handle character literal
if (c == '\'') {
tok_char_next();
c = tok_char_get();
tok->type = TOK_TYPE_NUM;
tok->text = input_ptr - 1;
tok->len = 3;
tok->val = c;
tok_char_next();
c = tok_char_get();
if (c != '\'') error("expected closing quote for character literal");
tok_char_next();
return;
}

tok->type = ('0' <= c && c <= '9') ? TOK_TYPE_NUM : TOK_TYPE_SYM;
tok->text = input_ptr;
tok->len = 1;
Expand Down
12 changes: 12 additions & 0 deletions sectorc.s
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ tok_next:
cmp al,32 ; skip spaces (anything <= ' ' is considered space)
jle tok_next

cmp al,39 ; check for single quote (character literal)
je _char_literal

xor bx,bx ; zero token reg
xor cx,cx ; zero last-two chars reg

Expand Down Expand Up @@ -356,6 +359,15 @@ _done:
mov ax,bx ; return token in ax also
ret

_char_literal:
call getch ; get the character inside quotes
xor ah,ah ; zero high byte of ax
mov bx,ax ; bx = character value
call getch ; consume closing quote
mov dl,1 ; tok_is_num = true (treat as number)
mov ax,bx ; return value in ax
ret

_comment_double_slash:
call getch ; get next char
cmp al,10 ; check for newline '\n'
Expand Down