From 6857c57397a3483cd0d839d41cfbe20a92da9e4a Mon Sep 17 00:00:00 2001 From: pycsham <shampuiyanchloe@gmail.com> Date: Mon, 10 Feb 2020 18:57:57 -0500 Subject: [PATCH] Fixed the J1 integer range checking problems --- Scanning.py | 3 ++- Weeding.py | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Scanning.py b/Scanning.py index 24226b4..3df1440 100644 --- a/Scanning.py +++ b/Scanning.py @@ -478,7 +478,7 @@ def scan(input): # Checking integer range (does not cover all edge cases) elif token.name == 'NUM' and index > 0 and tokens[index-1].name == 'SUB' and int(token.lex) > 2147483648: return (None, "integer too small") - elif token.name == 'NUM' and int(token.lex) > 2147483647: + elif token.name == 'NUM' and int(token.lex) > 2147483647 and (index is 0 or tokens[index-1].name is not 'SUB'): return (None, "interger too large") @@ -514,4 +514,5 @@ def scan(input): # remove whitespace, newline characters and comments tokens = list(filter(lambda t: t.name not in ("WHITESPACE", "COMMENT", 'LCOMMENT', 'RCOMMENT', 'NEWLINE', 'NEWLINEC', 'LCOM2', 'LCOM3'), tokens)) + return (tokens, "success") diff --git a/Weeding.py b/Weeding.py index 0b86e7a..72ec68e 100644 --- a/Weeding.py +++ b/Weeding.py @@ -5,6 +5,7 @@ import os def weedTokens(tokens, file): result = fileNameCheck(tokens, file) result += extendCheck(tokens) + result += intRangeCheck(tokens) return result @@ -32,6 +33,13 @@ def extendCheck(tokens): check = True return "" +def intRangeCheck(tokens): + for index, token in tokens: + if token.name == 'NUM' and index > 0 and tokens[index-1].name == 'SUB' and int(token.lex) > 2147483648: + return ("ERROR: integer too small") + if token.name == 'NUM' and int(token.lex) > 2147483647 and (index is 0 or tokens[index-1].name is not 'SUB'): + return ("ERROR: integer too large") + return "" ######################## Weeding after parsing ################################ -- GitLab