atexit.unregister(deinit)
def read(n):
- return str(os.read(sys.stdin.fileno(), n), 'ascii')
+ data = os.read(sys.stdin.fileno(), n)
+ if len(data) == 0:
+ raise Exception('end of input') # due to piping or input redirection
+ return str(data, 'ascii')
def write(str):
os.write(sys.stdout.fileno(), bytes(str, 'ascii'))
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import apple_io
-import t_def
import element
+import lex_yy
import sys
+import t_def
import y_tab
-#import lex_yy
+EXIT_SUCCESS = 0
+EXIT_FAILURE = 1
+
#print('yylex()', lex_yy.yylex())
#print('yytext', f'"{lex_yy.yytext:s}"')
#assert False
-#print('y_tab.yyparse()')
-program = y_tab.yyparse(t_def.NodeProgram)
+if len(sys.argv) < 2:
+ print(f'usage: {sys.argv[0]:s} program.bas')
+ sys.exit(EXIT_FAILURE)
+program_bas = sys.argv[1]
+
+with open(program_bas) as fin:
+ #print('y_tab.yyparse()')
+ lex_yy.yyin = fin
+ program = y_tab.yyparse(t_def.NodeProgram)
#print('element.serialize()')
#element.serialize(program, sys.stdout)
class NodeStatementFlash: NodeStatement;
class NodeStatementHTab: NodeStatement;
class NodeStatementVTab: NodeStatement;
+class NodeStatementGet: NodeStatement;
class NodeExpression: Node;
class NodeExpressionOr: NodeExpression;
class NodeExpressionAnd: NodeExpression;
if value[i] != '.':
i += 1
value = sign + value[:i] + exponent
- print(value, end = '')
+ apple_io.write(value)
if not self.semicolon:
- print()
+ apple_io.write('\r\n')
@method(NodeStatementGoto)
def execute(self, context):
context.i = context.find_line(self.children[0].int_value)
f'?TYPE MISMATCH ERROR IN {context.line_number():d}'
)
apple_io.vtab(int(math.floor(value)))
+@method(NodeStatementGet)
+def execute(self, context):
+ name = self.children[0].str_value
+ value = apple_io.read(1)
+ if name[-1] != '$':
+ raise Exception(
+ f'?SYNTAX ERROR IN {context.line_number():d}'
+ )
+ context.variables[name] = value
del execute
@method(NodeExpression)
| %space (?E{t_def.NodeStatementFlash}KEYWORD_FLASH)
| %space (?E{t_def.NodeStatementHTab}KEYWORD_HTAB expression)
| %space (?E{t_def.NodeStatementVTab}KEYWORD_VTAB expression)
+ | %space (?E{t_def.NodeStatementGet}KEYWORD_GET VARIABLE)
;
print_expression_list0
20 HTAB 10
30 PRINT "MY PROGRAM"
40 VTAB 5
-50 FLASH
-60 PRINT "GOODBYE"
-70 NORMAL
+50 PRINT "TYPE SOMETHING: ";
+60 GET I$
+70 PRINT "YOU TYPED: "I$
+90 FLASH
+100 PRINT "GOODBYE"
+110 NORMAL