From 9f54610fbda6dd4ebe5a273d519fa599ae6a2123 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Thu, 17 Oct 2013 05:17:30 -0400 Subject: [PATCH] Implement support for octal constants, this closes #97. --- lexer.c | 15 ++++++++++++--- tests/octal.qc | 11 +++++++++++ tests/octal.tmpl | 8 ++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/octal.qc create mode 100644 tests/octal.tmpl diff --git a/lexer.c b/lexer.c index 6c8ccd9..b6d5ceb 100644 --- a/lexer.c +++ b/lexer.c @@ -766,6 +766,7 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote) int ch = 0; int nextch; bool hex; + bool oct; char u8buf[8]; /* way more than enough */ int u8len, uc; @@ -851,17 +852,18 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote) chr = 0; nextch = lex_getch(lex); hex = (nextch == 'x'); - if (!hex) + oct = (nextch == '0'); + if (!hex && !oct) lex_ungetch(lex, nextch); for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) { - if (!hex) { + if (!hex && !oct) { if (nextch >= '0' && nextch <= '9') chr = chr * 10 + nextch - '0'; else { lexerror(lex, "bad character code"); return (lex->tok.ttype = TOKEN_ERROR); } - } else { + } else if (!oct) { if (nextch >= '0' && nextch <= '9') chr = chr * 0x10 + nextch - '0'; else if (nextch >= 'a' && nextch <= 'f') @@ -872,6 +874,13 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote) lexerror(lex, "bad character code"); return (lex->tok.ttype = TOKEN_ERROR); } + } else { + if (nextch >= '0' && nextch <= '9') + chr = chr * 8 + chr - '0'; + else { + lexerror(lex, "bad character code"); + return (lex->tok.ttype = TOKEN_ERROR); + } } if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255)) { diff --git a/tests/octal.qc b/tests/octal.qc new file mode 100644 index 0000000..bddb882 --- /dev/null +++ b/tests/octal.qc @@ -0,0 +1,11 @@ +void main() { + float a = 012; + float b = 0204; + float c = 076663; + float d = 0777; + + print(ftos(a), "\n"); + print(ftos(b), "\n"); + print(ftos(c), "\n"); + print(ftos(d), "\n"); +} diff --git a/tests/octal.tmpl b/tests/octal.tmpl new file mode 100644 index 0000000..45c7332 --- /dev/null +++ b/tests/octal.tmpl @@ -0,0 +1,8 @@ +I: octal.qc +D: test octal constants +T: -execute +C: -std=gmqcc +M: 10 +M: 132 +M: 32179 +M: 511 -- 2.39.2