mirror of
https://github.com/adamdunkels/ubasic.git
synced 2026-11-20 11:39:43 +00:00
Moved VARIABLE_TYPE to vartype.h. VARIABLE_TYPE functions.
The functions in ubasic.c that used int for variable values and the function expr() in tokenizer.c now use VARIABLE_TYPE. Also added a new test in tests.c.
This commit is contained in:
parent
a519c98e6c
commit
ea7a52e61f
7 changed files with 83 additions and 30 deletions
2
Makefile
2
Makefile
|
|
@ -1,2 +1,4 @@
|
|||
use-ubasic: use-ubasic.o ubasic.o tokenizer.o
|
||||
tests: tests.o ubasic.o tokenizer.o
|
||||
clean:
|
||||
rm *.o tests use-ubasic
|
||||
|
|
|
|||
25
tests.c
25
tests.c
|
|
@ -38,16 +38,25 @@ static const char program_loop[] =
|
|||
"10 for i = 0 to 126\n\
|
||||
20 for j = 0 to 126\n\
|
||||
30 for k = 0 to 10\n\
|
||||
37 let a = i * j * k\n\
|
||||
40 next k\n\
|
||||
50 next j\n\
|
||||
60 next i\n\
|
||||
70 end\n";
|
||||
40 let a = i * j * k\n\
|
||||
50 next k\n\
|
||||
60 next j\n\
|
||||
70 next i\n\
|
||||
80 end\n";
|
||||
|
||||
static const char program_let[] =
|
||||
"10 let a = 42\n\
|
||||
20 end\n";
|
||||
|
||||
static const char program_fibs[] =
|
||||
"20 let a = 1\n\
|
||||
40 let b = 1\n\
|
||||
60 for i = 0 to 8\n\
|
||||
80 let b = a + b\n\
|
||||
100 let a = b - a\n\
|
||||
140 next i\n\
|
||||
160 end\n";
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void run(const char program[]) {
|
||||
|
|
@ -70,7 +79,7 @@ void run(const char program[]) {
|
|||
end_t = clock();
|
||||
delta_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("done. Run time: %.3f\n", delta_t);
|
||||
printf("done. Run time: %.3f s\n", delta_t);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
@ -79,9 +88,13 @@ main(void)
|
|||
{
|
||||
run(program_let);
|
||||
assert(ubasic_get_variable(0) == 42);
|
||||
|
||||
run(program_loop);
|
||||
assert(ubasic_get_variable(0) == (VARIABLE_TYPE)(126 * 126 * 10));
|
||||
|
||||
run(program_fibs);
|
||||
assert(ubasic_get_variable(1) == 89);
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
|
|||
32
tokenizer.c
32
tokenizer.c
|
|
@ -43,7 +43,7 @@
|
|||
|
||||
static char const *ptr, *nextptr;
|
||||
|
||||
#define MAX_NUMLEN 5
|
||||
#define MAX_NUMLEN 6
|
||||
|
||||
struct keyword_token {
|
||||
char *keyword;
|
||||
|
|
@ -118,21 +118,21 @@ get_next_token(void)
|
|||
if(*ptr == 0) {
|
||||
return TOKENIZER_ENDOFINPUT;
|
||||
}
|
||||
|
||||
|
||||
if(isdigit(*ptr)) {
|
||||
for(i = 0; i < MAX_NUMLEN; ++i) {
|
||||
if(!isdigit(ptr[i])) {
|
||||
if(i > 0) {
|
||||
nextptr = ptr + i;
|
||||
return TOKENIZER_NUMBER;
|
||||
} else {
|
||||
DEBUG_PRINTF("get_next_token: error due to too short number\n");
|
||||
return TOKENIZER_ERROR;
|
||||
}
|
||||
if(i > 0) {
|
||||
nextptr = ptr + i;
|
||||
return TOKENIZER_NUMBER;
|
||||
} else {
|
||||
DEBUG_PRINTF("get_next_token: error due to too short number\n");
|
||||
return TOKENIZER_ERROR;
|
||||
}
|
||||
}
|
||||
if(!isdigit(ptr[i])) {
|
||||
DEBUG_PRINTF("get_next_token: error due to malformed number\n");
|
||||
return TOKENIZER_ERROR;
|
||||
DEBUG_PRINTF("get_next_token: error due to malformed number\n");
|
||||
return TOKENIZER_ERROR;
|
||||
}
|
||||
}
|
||||
DEBUG_PRINTF("get_next_token: error due to too long number\n");
|
||||
|
|
@ -150,8 +150,8 @@ get_next_token(void)
|
|||
} else {
|
||||
for(kt = keywords; kt->keyword != NULL; ++kt) {
|
||||
if(strncmp(ptr, kt->keyword, strlen(kt->keyword)) == 0) {
|
||||
nextptr = ptr + strlen(kt->keyword);
|
||||
return kt->token;
|
||||
nextptr = ptr + strlen(kt->keyword);
|
||||
return kt->token;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -161,7 +161,7 @@ get_next_token(void)
|
|||
return TOKENIZER_VARIABLE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return TOKENIZER_ERROR;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
@ -196,7 +196,7 @@ tokenizer_next(void)
|
|||
return;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
VARIABLE_TYPE
|
||||
tokenizer_num(void)
|
||||
{
|
||||
return atoi(ptr);
|
||||
|
|
@ -207,7 +207,7 @@ tokenizer_string(char *dest, int len)
|
|||
{
|
||||
char *string_end;
|
||||
int string_len;
|
||||
|
||||
|
||||
if(tokenizer_token() != TOKENIZER_STRING) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,8 @@
|
|||
#ifndef __TOKENIZER_H__
|
||||
#define __TOKENIZER_H__
|
||||
|
||||
#include "vartype.h"
|
||||
|
||||
enum {
|
||||
TOKENIZER_ERROR,
|
||||
TOKENIZER_ENDOFINPUT,
|
||||
|
|
@ -69,7 +71,7 @@ enum {
|
|||
void tokenizer_init(const char *program);
|
||||
void tokenizer_next(void);
|
||||
int tokenizer_token(void);
|
||||
int tokenizer_num(void);
|
||||
VARIABLE_TYPE tokenizer_num(void);
|
||||
int tokenizer_variable_num(void);
|
||||
void tokenizer_string(char *dest, int len);
|
||||
|
||||
|
|
|
|||
8
ubasic.c
8
ubasic.c
|
|
@ -64,7 +64,7 @@ static VARIABLE_TYPE variables[MAX_VARNUM];
|
|||
|
||||
static int ended;
|
||||
|
||||
static int expr(void);
|
||||
static VARIABLE_TYPE expr(void);
|
||||
static void line_statement(void);
|
||||
static void statement(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
@ -156,7 +156,7 @@ term(void)
|
|||
return f1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
static VARIABLE_TYPE
|
||||
expr(void)
|
||||
{
|
||||
int t1, t2;
|
||||
|
|
@ -479,14 +479,14 @@ ubasic_finished(void)
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
ubasic_set_variable(int varnum, int value)
|
||||
ubasic_set_variable(int varnum, VARIABLE_TYPE value)
|
||||
{
|
||||
if(varnum >= 0 && varnum <= MAX_VARNUM) {
|
||||
variables[varnum] = value;
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
VARIABLE_TYPE
|
||||
ubasic_get_variable(int varnum)
|
||||
{
|
||||
if(varnum >= 0 && varnum <= MAX_VARNUM) {
|
||||
|
|
|
|||
6
ubasic.h
6
ubasic.h
|
|
@ -30,13 +30,13 @@
|
|||
#ifndef __UBASIC_H__
|
||||
#define __UBASIC_H__
|
||||
|
||||
#define VARIABLE_TYPE char
|
||||
#include "vartype.h"
|
||||
|
||||
void ubasic_init(const char *program);
|
||||
void ubasic_run(void);
|
||||
int ubasic_finished(void);
|
||||
|
||||
int ubasic_get_variable(int varnum);
|
||||
void ubasic_set_variable(int varum, int value);
|
||||
VARIABLE_TYPE ubasic_get_variable(int varnum);
|
||||
void ubasic_set_variable(int varum, VARIABLE_TYPE value);
|
||||
|
||||
#endif /* __UBASIC_H__ */
|
||||
|
|
|
|||
36
vartype.h
Normal file
36
vartype.h
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright (c) 2006, Adam Dunkels
|
||||
* Copyright (c) 2013, Danyil Bohdan
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the author nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
#ifndef __VARTYPE_H__
|
||||
#define __VARTYPE_H__
|
||||
|
||||
#define VARIABLE_TYPE char
|
||||
|
||||
#endif /* __VARTYPE_H__ */
|
||||
Loading…
Add table
Add a link
Reference in a new issue