like to see what it looks like. Reading your source code I can find out
what language it is you are implementing (jsb_context tells tales).
But a grammar could tell me what a .jsb file is all about a lot faster.
A grammar for the json basic scripting language could look
something like this (it's far from complete)
Code: Select all
''Lexical grammar
''single character tokens
AMPERSAND ::= '&'
ASSIGN ::= '='
CARET ::= '^'
COLON ::= ':'
COMMA ::= ','
DIVIDE ::= '/'
DOT ::= '.'
DQUOTE ::= '"'
FSLASH ::= '\'
GT ::= '>'
LBRACK ::= '['
LCURLY ::= '{'
LPAREN ::= '('
LT ::= '<'
MINUS ::= '-'
MUL ::= '*'
PLUS ::= '+'
RBRACK ::= ']'
RCURLY ::= '}'
RPAREN ::= ')'
SEMI ::= ';'
SQUOTE ::= "'"
UNDERSCORE ::= '_'
''ranges of single character tokens
LOWER ::= 'a'
''many lines LOWER ::= 'b' UPPER ::= 'c' etc...
UPPER ::= 'A'
''many lines UPPER ::= 'B' UPPER ::= 'C' etc...
DIGIT ::= '0'
DIGIT ::= '1'
DIGIT ::= '2'
DIGIT ::= '3'
DIGIT ::= '4'
DIGIT ::= '5'
DIGIT ::= '6'
DIGIT ::= '7'
DIGIT ::= '8'
DIGIT ::= '9'
IDENTIFIER ::= UNDERSCORE IDENTIFIER1
IDENTIFIER ::= UPPER
IDENTIFIER ::= LOWER
IDENTIFIER ::= UPPER IDENTIFIER1
IDENTIFIER ::= LOWER IDENTIFIER1
IDENTIFIER1 ::= UPPER
IDENTIFIER! ::= LOWER
IDENTIFIER1 ::= DIGIT
IDENTIFIER1 ::= UNDERSCORE
IDENTIFIER1 ::= UPPER IDENTIFIER1
IDENTIFIER1 ::= LOWER IDENTIFIER1
IDENTIFIER1 ::= DIGIT IDENTIFIER1
IDENTIFIER1 ::= UNDERSCORE IDENTIFIER1
NUMBER ::= DIGIT
NUMBER ::= DIGIT NUMBER
''string definition
STRING ::= DQUOTE STRING_CHARS DQUOTE
STRING_CHARS ::= CHAR
STRING_CHARS ::= CHAR STRING_CHARS
''CHAR definition
CHAR ::= ESCAPED
CHAR ::= UNDERSCORE
CHAR ::= DIGIT
CHAR ::= UPPER
CHAR ::= LOWER
ESCAPED ::= FSLASH ESCAPED_CHARS
ESCAPED_CHARS ::= FSLASH
ESCAPED_CHARS ::= DIGIT
ESCAPED_CHARS ::= UPPER
ESCAPED_CHARS ::= LOWER
''many more alternatives
''combinations of assign and some other operator (many more entries needed)
ASSIGN_OP ::= '+='
ASSING_OP ::= '-='
''keywords
VAR ::= 'var'
FOR ::= 'for'
TO ::= 'to'
WHILE ::= 'while'
''context free(ish) grammar
program ::= statement_list
statement_list ::= statement
statement_list ::= statement_list statement
statement ::= expression
statement ::= declaration
statement ::= if_statement
statement ::= for_statement
statement ::= compound_statement
statement ::= while_statement
expression ::= expression COMMA expression
expression ::= expression ASSIGN expression
expression ::= expression PLUS expression
expression ::= expression MINUS expression
expression ::= expression MUL expression
expression ::= expression CARET expression
expression ::= expression DIVIDE expression
expression ::= expression LT expression
expression ::= expression GT expression
expression ::= expression LT ASSIGN expression
expression ::= expression GT ASSIGN expression
expression ::= expression LCURLY expression RCURLY
expression ::= expression LBRACK expression RBRACK
expression ::= expression LPAREN expression RPAREN
expression ::= expression ASSIGN_OP expression
expression ::= expression COLON expression
expression ::= expression LT expression
expression ::= expression GT expression
expression ::= expression LT GT expression
expression ::= expression AMPERSAND expression
expression ::= expression DOT expression
expression ::= PLUS expression
expression ::= MINUS expression
expression ::= NUMBER
expression ::= STRING
expression ::= IDENTIFIER
declaration ::= VAR IDENTIFIER ASSIGN expression SEMI
declaration ::= VAR IDENTIFIER SEMI
if_statement ::= IF expression statement
if_statement ::= IF expression statement ELSE statement
compound_statement ::= LCURLY statement RCURLY SEMI
for_statement ::= FOR IDENTIFIER ASSIGN expression TO expression statement
while_statement ::= WHILE expression statement
More productions and terminals are needed (lots more binary operators) to somewhat
complete the grammar.
I am not sure whether { statement }; is allowed at the top level.
Code: Select all
x = 55;
{
var a = 4;
var b = a + 8;
};
The above would introduce a scope outside the context of an if, while, for etc....
(same as scope... end scope in FB).
As json basic scripting uses the JSON format it could be worthwhile for users to
read the JSON grammar
http://tools.ietf.org/html/rfc7159
It is very nice to see a project dedicated to writing an interpreter in FreeBASIC.
I would not mind at all if there would be lots more projects dedicated to
writing an interpreter in FB.
As an aside (or a PS): having read your source code I am guessing that there is
room for improvement of the performance of the interpreter.