Skip to content

Commit

Permalink
Added support for DATA statement
Browse files Browse the repository at this point in the history
  • Loading branch information
richpl committed Jan 19, 2019
1 parent a46583b commit 9590950
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion basicparser.py
Expand Up @@ -5,14 +5,15 @@
import math
import random


"""Implements a BASIC array, which may have up
to three dimensions of fixed size.
"""
class BASICArray:

def __init__(self, dimensions):
"""Initialises te object with the specified
"""Initialises the object with the specified
number of dimensions. Maximum number of
dimensions is three
Expand Down Expand Up @@ -53,6 +54,9 @@ def __init__(self):
# when evaluating expressions
self.__operand_stack = []

# List to hold contents of DATA statement
self.__data_values = []

# These values will be
# initialised on a per
# statement basis
Expand Down Expand Up @@ -165,6 +169,10 @@ def __simplestmt(self):
self.__randomizestmt()
return None

elif self.__token.category == Token.DATA:
self.__datastmt()
return None

else:
# Ignore comments, but raise an error
# for anything else
Expand Down Expand Up @@ -433,6 +441,21 @@ def __inputstmt(self):
# No more input to process
pass

def __datastmt(self):
"""Parses a DATA statement"""

self.__advance() # Advance past DATA token

# Acquire the comma separated values
if not self.__tokenindex >= len(self.__tokenlist):
self.__expr()
self.__data_values.append(self.__operand_stack.pop())

while self.__token.category == Token.COMMA:
self.__advance() # Advance past comma
self.__expr()
self.__data_values.append(self.__operand_stack.pop())

def __expr(self):
"""Parses a numerical expression consisting
of two terms being added or subtracted,
Expand Down

0 comments on commit 9590950

Please sign in to comment.