Вграждаемо интерпретаторче на С
За една моя бъдеща система ми трябваше нещо за скриптове и затова написах интерпретатор на С. Доста време ми отне - около 7 месеца. Сорса е общо само около 3000 реда.
Изненадващо малко такива интерпретатори има опън-сорс. Всъщност май има само един дето да става - пико-с, обаче и при него имаше неща дето не ми харесаха, пък и бавничък и тежък ми се стори, та затова реших аз да напиша един от нулата. Естествено мисля да го пусна изцяло опън сорс, дано да свърши работа и на други.
Обаче първо ми трябва малко помощ в бета тестовете. Аз колкото съм могъл съм изтествал всичко, обаче... все пак за С става въпрос.
Или накратко - молбата ми е, намерете му още бъгове
Пускам текущия дебъг билд като ехе (иначе е напълно платформено независим, понеже на мен ми трябва за микроконтролер). Естествено TXT разширението трябва да се махне
За теста просто му се дава С програма като параметър при изпълнение.
Ето извадка от главния хедър с "документация":
| | |
| Код: /* ======================================================================================
Written and debugged with Dev-C++ v5.11 NOTE: for best source readability, please use a monospace font (eg. Consolas)
The aim of this code is to achieve as full as possible functional implementation of an interpreter for the C language with minimum required system resources and without references to specific external layers such as OS.
Data types:
'bool' - defined as 1-bit integer 'char' - defined as 8-bit integer 'short' or 'short int' - defined as 16-bit integer 'int' - defined as per the 'typedef int_t' statement 'long' or 'long int' - defined as 32-bit integer 'long long' or 'long long int' - defined as 64-bit integer 'float' - single precision floating point number (32-bit) 'double' - double precision floating point number (64-bit) 'long double' - quadruple precision floating point number (80-bit or more)
In addition to the basic types above, several other data types are also supported:
'size_t' - natively represented as 'unsigned int' 'enum' - standard C-type enum list 'struct' - standard C-type data structure 'union' - standard C-type data union 'va_list' - used in conjunction with the <stdarg.h> library for variable argument lists 'FILE' - used with file functions in the <stdio.h> library
'signed' or 'unsigned' optionally preceding the integer types, except 'bool' All integer types are signed by default (i.e. as if preceded by a 'signed' declaration)
Constant data types:
'char' - single 8-bit character "string" - string of zero-terminated 8-bit characters up to 64-bit decimal integer numbers (optionally preceded by '0d' or '0D') up to 64-bit hexadecimal unsigned numbers; preceded by '0x' or '0X' up to 64-bit binary unsigned numbers; preceded by '0b' or '0B' up to 64-bit octal unsigned numbers; preceded by '0' floating point numbers (always decimal) in format: [sign] [ iii [ .fff ['E/e' [sign] [ eee [ .xxx ] ] ] ] ]
Decimal numbers optionally support suffix specifiers for sign and/or size: 'u' or 'U' for unsigned 'l' or 'L' for long integer or long double 'll' or 'LL' for long long integer 'f' or 'F' for float 'd' or 'D' for double
The default type of all integer constants, unless explicitly specified, is 'signed int' The default type of floating point number constants, unless explicitly specified, is 'float'
Pre-defined character literals:
'\a' - ASCII code 7 (alarm) '\b' - ASCII code 8 (backspace) '\e' - ASCII code 27 (escape) '\f' - ASCII code 12 (form feed) '\n' - ASCII code 10 (new line) '\r' - ASCII code 13 (carriage return) '\t' - ASCII code 9 (horizontal tabulation) '\\' - the character 'backslash' '\'' - the character 'single quote' '\"' - the character 'double quote' '\?' - the character 'question mark'
Other notes and features:
1. True execution in place. The source code is not compiled or modified and can run directly from ROM 2. Fully dynamic memory model with no limitation on number of functions or variables, or their size 3. Inline local variable definitions are allowed, eg. for(int t=0; ..... 4. String literals can be split on to multiple lines 5. Pointer to constant (eg. const char *) and constant pointer (eg. char const *), are both supported 6. Standard and // commentaries are both supported
Built-in libraries:
<stdlib.h> <stdio.h> <stdint.h> <stdbool.h> <stdarg.h> <string.h> <math.h> <limits.h> <ctype.h>
Known issues and limitations:
1. Function pointers and function address dereferencing are not supported (no feasible way in an interpreter!) 2. Preprocessor directives are reduced to only #include of pre-built libraries 3. In for(), while(), and do...while() loops, 'break' and 'continue' can only work from within a {} block 4. The 'goto' instruction only allows jumping to a same {} depth level location 5. The 'extern' keyword is not supported since the source can be only a single file 6. main() function must not have parameters
Other notes:
The entire code is deeply recursive so it requires sufficient stack for its operation. With the deflault settings, at least 2048 bytes stack size is recommended.
=========================================================================================== */
| |
| | |