Implement console input, improve console output a bit (use write not print)
authorNick Downing <nick@ndcode.org>
Mon, 16 May 2022 10:21:28 +0000 (20:21 +1000)
committerNick Downing <nick@ndcode.org>
Mon, 16 May 2022 10:21:28 +0000 (20:21 +1000)
apple_io.py
applesoft_basic.py
applesoft_basic.t
applesoft_basic.y
test_io.bas

index c49b30e..e5e2a62 100755 (executable)
@@ -22,7 +22,10 @@ def deinit():
     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'))
index 6662c3d..cc4c2e8 100755 (executable)
 # 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)
index a09e72e..0a05c7d 100644 (file)
@@ -69,6 +69,7 @@ class NodeStatementInverse: NodeStatement;
 class NodeStatementFlash: NodeStatement;
 class NodeStatementHTab: NodeStatement;
 class NodeStatementVTab: NodeStatement;
+class NodeStatementGet: NodeStatement;
 class NodeExpression: Node;
 class NodeExpressionOr: NodeExpression;
 class NodeExpressionAnd: NodeExpression;
@@ -294,9 +295,9 @@ def execute(self, context):
       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)
@@ -474,6 +475,15 @@ def execute(self, context):
       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)
index 9e764da..fdb219b 100644 (file)
@@ -176,6 +176,7 @@ statement_opt
   | %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
index 2087f5e..4f86808 100644 (file)
@@ -2,6 +2,9 @@
 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