sectcl/quecl.c

65 lines
1.6 KiB
C

// Quecl - a quecto-sized Tcl-inspired programming language
#include <stddef.h>
#include <stdio.h>
#define MEM_SIZE 32 * 1024 // 32 kilobytes
char MEMORY[MEM_SIZE];
size_t basepointer = 0;
size_t stack = 0;
size_t GetLine() {
// reads a line of user input into MEMORY
char c;
size_t ptr = stack;
do {
c = getchar();
MEMORY[stack++] = c;
} while (c != '\n');
MEMORY[stack++] = 0;
return ptr;
}
size_t GetVerbatim(size_t start) {
// Verbatim words are words that start with {
// Braces nest and can be escaped
unsigned int level = 0; // first char is {
size_t ptr = stack;
char c;
do {
c = MEMORY[start++];
// TODO: implement \-escaping
if(c == '{') level++;
if(c == '}') level--;
MEMORY[stack++] = 0;
} while(c != 0 && level > 0);
MEMORY[stack++] = 0;
return ptr;
}
size_t GetWord(size_t start) {
// Start indicates a memory address
// Returns a single "word"
// Rules of tcl words: [] (and $) work everywhere
// having { at the start means that the string is verbatim
// {} can be nested
// [] is command substitution: essentially calling eval
if(MEMORY[start] == '{') return GetVerbatim(start);
int quote = MEMORY[start] == '"';
char c;
size_t ptr = stack;
do {
c = MEMORY[start++];
// TODO: implement [] and maybe $
if(c == '\\') c = MEMORY[start++];
// TODO: implement 'smarter' escaping, e.g. \n
MEMORY[stack++] = c;
} while(c != '"' || (quote && c <= ' '));
return ptr;
}
int main() {
}