* Tokens
*/
-// any token before 256 is just the literal char value
-enum ulas_toks { ULAS_TOKLITERAL = 256, ULAS_TOKINT, ULAS_TOKCHAR, ULAS_TOKSTRING };
+// any token before 256 is just the literal char value
+enum ulas_toks { ULAS_TOKLITERAL = 256, ULAS_TOKSYMBOL };
// primitive data types
enum ulas_type { ULAS_INT, ULAS_STR };
int ulas_tok(struct ulas_str *dst, const char **out_line, unsigned long n);
// converts a token string to a token struct
-struct ulas_tok ulas_totok(const char *buf, unsigned long n, int *rc);
+struct ulas_tok ulas_totok(char *buf, unsigned long n, int *rc);
int ulas_tokuntil(struct ulas_str *dst, char c, const char **out_line,
unsigned long n);
#include "ulas.h"
#include <ctype.h>
#include <errno.h>
+#include <stdlib.h>
#include <string.h>
#include <assert.h>
return i;
}
-struct ulas_tok ulas_totok(const char *buf, unsigned long n, int *rc) {
+struct ulas_tok ulas_totok(char *buf, unsigned long n, int *rc) {
struct ulas_tok tok;
memset(&tok, 0, sizeof(tok));
}
unsigned char first = buf[0];
+ buf++;
switch (first) {
case ';':
default:
if (isdigit(first)) {
// integer
+ tok.type = ULAS_TOKLITERAL;
+ tok.lit.type = ULAS_INT;
+ tok.lit.val.int_value = (int)strtol(buf, &buf, 0);
+ } else if (n == 3 && first == '\'') {
+ tok.type = ULAS_TOKLITERAL;
+ tok.lit.type = ULAS_INT;
+ // TODO: read char value between ' and ' and unescape
} else if (ulas_isname(buf, n)) {
// literal. we can resolve it now
// because literals need to be able to be resolved
// for every line, unless they are a label!
+ // TODO: read and unescape striing between " and "
+ tok.type = ULAS_TOKSYMBOL;
+ tok.lit.type = ULAS_STR;
} else {
ULASERR("Unexpected token: %s\n", buf);
}
end:
+ // did we consume the entire token?
+ if (buf[0] != '\0') {
+ *rc = -1;
+ }
+
return tok;
}