changeset 34:bfea77812e64

Start of assignment code
author Lost Wizard (lost@starbug3)
date Fri, 04 Feb 2011 21:27:03 -0700
parents 890a8f688889
children cdb0175e1063
files lwbasic/lexer.c lwbasic/lwbasic.h lwbasic/parser.c
diffstat 3 files changed, 89 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/lwbasic/lexer.c	Thu Feb 03 22:15:57 2011 -0700
+++ b/lwbasic/lexer.c	Fri Feb 04 21:27:03 2011 -0700
@@ -59,9 +59,39 @@
 	{ "endsub",			token_kw_endsub },
 	{ "endfunction",	token_kw_endfunction },
 	{ "dim",			token_kw_dim },
+	{ "=",				token_op_assignment },
 	{ NULL }
 };
 
+static char *lexer_token_names[] =
+{
+	"SUB",
+	"FUNCTION",
+	"AS",
+	"PUBLIC",
+	"PRIVATE",
+	"PARAMS",
+	"RETURNS",
+	"INTEGER",
+	"ENDSUB",
+	"ENDFUNCTION",
+	"DIM",
+	"<assignment>",
+	"<identifier>",
+	"<char>",
+	"<uint>",
+	"<int>",
+	"<eol>",
+	"<eof>"
+};
+
+char *lexer_token_name(int token)
+{
+	if (token > token_eol)
+		return "???";
+	return lexer_token_names[token];
+}
+
 static int lexer_getchar(cstate *state)
 {
 	int c;
@@ -231,12 +261,12 @@
 		buflen = 128;
 	}
 
-	l = snprintf(buffer, buflen, "%s (%d)", state -> lexer_token_string, state -> lexer_token);
+	l = snprintf(buffer, buflen, "%s (%s)", state -> lexer_token_string, lexer_token_name(state -> lexer_token));
 	if (l >= buflen)
 	{
 		buffer = lw_realloc(buffer, l + 1);
 		buflen = l + 1;
-		snprintf(buffer, buflen, "%s (%d)", state -> lexer_token_string, state -> lexer_token);
+		snprintf(buffer, buflen, "%s (%s)", state -> lexer_token_string, lexer_token_name(state -> lexer_token));
 	}
 	return buffer;
 }
--- a/lwbasic/lwbasic.h	Thu Feb 03 22:15:57 2011 -0700
+++ b/lwbasic/lwbasic.h	Fri Feb 04 21:27:03 2011 -0700
@@ -86,6 +86,7 @@
 	token_kw_endsub,			/* ENDSUB keyword */
 	token_kw_endfunction,		/* ENDFUNCTION keyword */
 	token_kw_dim,				/* DIM keyword */
+	token_op_assignment,		/* assignment operator */
 	token_identifier,			/* an identifier (variable, function, etc. */
 	token_char,					/* single character; fallback */
 	token_uint,					/* unsigned integer up to 32 bits */
@@ -114,6 +115,7 @@
 #ifndef __lexer_c_seen__
 extern void lexer(cstate *state);
 extern char *lexer_return_token(cstate *state);
+extern char *lexer_token_name(int token);
 #endif
 
 #ifndef __emit_c_seen__
--- a/lwbasic/parser.c	Thu Feb 03 22:15:57 2011 -0700
+++ b/lwbasic/parser.c	Fri Feb 04 21:27:03 2011 -0700
@@ -31,6 +31,13 @@
 #include "lwbasic.h"
 #include "symtab.h"
 
+static void expect(cstate *state, int tt)
+{
+	if (state -> lexer_token != tt)
+		lwb_error("Expecting %s, got %s\n", lexer_token_name(tt), lexer_return_token(state));
+	lexer(state);
+}
+
 
 /* size of a type */
 static int sizeof_type(int type)
@@ -98,11 +105,57 @@
 			
 			lw_free(vn);
 			break;
+		
+		/* blank lines allowed */
+		case token_eol:
+			break;
+			
 		default:
 			return;
 		}
 		if (state -> lexer_token != token_eol)
-			lwb_error("Expecting end of line; got %s", lexer_return_token(state));
+			lwb_error("Expecting end of line; got %s\n", lexer_return_token(state));
+		lexer(state);
+	}
+}
+
+static void parse_statements(cstate *state)
+{
+	symtab_entry_t *se;
+	
+	for (;;)
+	{
+		switch (state -> lexer_token)
+		{
+		/* blank lines allowed */
+		case token_eol:
+			break;
+		
+		/* variable assignment */
+		case token_identifier:
+			se = symtab_find(state -> local_syms, state -> lexer_token_string);
+			if (!se)
+			{
+				se = symtab_find(state -> global_syms, state -> lexer_token_string);
+			}
+			if (!se)
+				lwb_error("Unknown variable %s\n", state -> lexer_token_string);
+			lexer(state);
+			expect(state, token_op_assignment);
+
+			/* parse the expression */
+			/* parse_expression(state); */
+			
+			/* actually do the assignment */
+			
+			break;
+		
+		/* anything we don't recognize as a statement token breaks out */
+		default:
+			return;
+		}
+		if (state -> lexer_token != token_eol)
+			lwb_error("Expecting end of line; got %s\n", lexer_return_token(state));
 		lexer(state);
 	}
 }
@@ -229,7 +282,7 @@
 	emit_prolog(state, vis);
 	
 	/* parse statement block  */
-	/* parse_statements(state); */
+	parse_statements(state);
 	
 	if (issub)
 	{