Skip to content

Instantly share code, notes, and snippets.

@PJensen
Created July 7, 2011 16:46
Show Gist options
  • Save PJensen/1069945 to your computer and use it in GitHub Desktop.
Save PJensen/1069945 to your computer and use it in GitHub Desktop.
The Scruit compiler.
import os, sys, string, random
COMPILER_ERRORS = """
Cannot have multiple default property/method in a Class
Cannot use parentheses when calling a Sub
Class initialize or terminate do not have arguments
'Default' specification can only be on Property Get
'Default' specification must also specify 'Public'
Expected '('
Expected ')'
Expected '='
Expected 'Case'
Expected 'Class'
Expected end of statement
Expected 'End'
Expected expression
Expected 'Function'
Expected identifier
Expected 'If'
Expected 'In'
Expected integer constant
Expected Let or Set or Get in property declaration
Expected literal constant
Expected 'Loop'
Expected 'Next'
Expected 'Property'
Expected 'Select'
Expected statement
Expected 'Sub'
Expected 'Then'
Expected 'To'
Expected 'Wend'
Expected 'While' or 'Until'
Expected 'While,' 'Until,' or end of statement
Expected 'With'
Identifier too long
Invalid character
Invalid 'exit' statement
Invalid 'for' loop control variable
Invalid number
Invalid use of 'Me' keyword
'loop' without 'do'
Must be defined inside a Class
Must be first statement on the line
Name redefined
Number of arguments must be consistent across properties specification
Out of Memory
Property Set or Let must have at least one argument
Syntax error
Unexpected 'Next'
Unterminated string constant"""
COMPILER_ERRORS = COMPILER_ERRORS.split('\n')
# Compiler variables
inFile = ''
inFileData = ''
# Get input file
try:
inFile = sys.argv[1]
except:
print 'Scruit: No input file.'
sys.exit(1)
# Read input file
try:
fp = open(inFile, 'r')
inFileData = fp.read()
fp.close()
except:
print 'Scruit: Failed reading input file.'
sys.exit(1)
# Parse input file.
try:
inFileLines = inFileData.split('\n')
inFileLines.remove('\n')
if (len(inFileLines) <= 0):
print 'Scruit: Input file empty.'
except:
print 'Scruit: Failed parsing with newlines.'
try:
lineCount = 0
for fileLine in inFileLines:
if (fileLine == ''):
continue
print str(lineCount) + ':\n' + fileLine + '\n'
print '-' * random.randint(0, len(fileLine)),
print '^\t' + random.choice(COMPILER_ERRORS) + '\n'
lineCount = lineCount + 1
except:
print 'Scruit: Fatal error, Scruit!'
print 'SUCCESS!'
@exallium
Copy link

exallium commented Jul 7, 2011

This. This is hilarious. At first I thought it was serious, until I read random.choice(COMPILER_ERRORS)

Do you know about enumerate(iterable) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment