changeset 33:890a8f688889

Basic parsing of local variable decls
author lost@l-w.ca
date Thu, 03 Feb 2011 22:15:57 -0700
parents 49d608aecc4d
children bfea77812e64
files lwbasic/lexer.c lwbasic/lwbasic.h lwbasic/parser.c
diffstat 3 files changed, 43 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/lwbasic/lexer.c	Thu Feb 03 22:00:47 2011 -0700
+++ b/lwbasic/lexer.c	Thu Feb 03 22:15:57 2011 -0700
@@ -58,6 +58,7 @@
 	{ "integer",		token_kw_integer },
 	{ "endsub",			token_kw_endsub },
 	{ "endfunction",	token_kw_endfunction },
+	{ "dim",			token_kw_dim },
 	{ NULL }
 };
 
--- a/lwbasic/lwbasic.h	Thu Feb 03 22:00:47 2011 -0700
+++ b/lwbasic/lwbasic.h	Thu Feb 03 22:15:57 2011 -0700
@@ -85,6 +85,7 @@
 	token_kw_integer,			/* INTEGER keyword */
 	token_kw_endsub,			/* ENDSUB keyword */
 	token_kw_endfunction,		/* ENDFUNCTION keyword */
+	token_kw_dim,				/* DIM keyword */
 	token_identifier,			/* an identifier (variable, function, etc. */
 	token_char,					/* single character; fallback */
 	token_uint,					/* unsigned integer up to 32 bits */
--- a/lwbasic/parser.c	Thu Feb 03 22:00:47 2011 -0700
+++ b/lwbasic/parser.c	Thu Feb 03 22:15:57 2011 -0700
@@ -62,10 +62,48 @@
 static void parse_decls(cstate *state)
 {
 	/* declarations */
-	switch (state -> lexer_token)
+	/* the first thing that doesn't look like a declaration is assumed */
+	/* to be a statement and will trigger a bailout */
+	int vt;
+	char *vn;
+	symtab_entry_t *se;
+	
+	for (;;)
 	{
-	default:
-		return;
+		switch (state -> lexer_token)
+		{
+		/* DIM keyword */
+		case token_kw_dim:
+			lexer(state);
+			if (state -> lexer_token != token_identifier)
+			{
+				lwb_error("Expecting identifier, got %s\n", lexer_return_token(state));
+			}
+			vn = lw_strdup(state -> lexer_token_string);
+			lexer(state);
+			if (state -> lexer_token != token_kw_as)
+			{
+				lwb_error("Expecting AS, got %s\n", lexer_return_token(state));
+			}
+			lexer(state);
+			vt = parse_type(state);
+			
+			se = symtab_find(state -> local_syms, vn);
+			if (se)
+			{
+				lwb_error("Multiply defined local variable %s", vn);
+			}
+			state -> framesize += sizeof_type(vt);
+			symtab_register(state -> local_syms, vn, -(state -> framesize), symtype_var, NULL);
+			
+			lw_free(vn);
+			break;
+		default:
+			return;
+		}
+		if (state -> lexer_token != token_eol)
+			lwb_error("Expecting end of line; got %s", lexer_return_token(state));
+		lexer(state);
 	}
 }