Create standard library proof of concept
This commit is contained in:
@@ -1,12 +1,7 @@
|
||||
from smnp.environment.environment import Environment
|
||||
from smnp.module import functions, methods
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
def createEnvironment():
|
||||
variables = {
|
||||
"bpm": Type.integer(120)
|
||||
}
|
||||
|
||||
return Environment([ variables ], functions, methods)
|
||||
return Environment([{}], functions, methods)
|
||||
|
||||
|
||||
0
smnp/library/__init__.py
Normal file
0
smnp/library/__init__.py
Normal file
0
smnp/library/code/__init__.py
Normal file
0
smnp/library/code/__init__.py
Normal file
9
smnp/library/loader.py
Normal file
9
smnp/library/loader.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from pkg_resources import resource_string
|
||||
|
||||
from smnp.program.interpreter import Interpreter
|
||||
|
||||
|
||||
def loadStandardLibrary():
|
||||
source = resource_string('smnp.library.code', 'main.mus').decode("utf-8")
|
||||
return Interpreter.interpretString(source)
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import sys
|
||||
|
||||
from smnp.error.base import SmnpException
|
||||
from smnp.library.loader import loadStandardLibrary
|
||||
from smnp.program.interpreter import Interpreter
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
|
||||
Interpreter.interpretFile(sys.argv[1], printAst=True)
|
||||
stdLibraryEnv = loadStandardLibrary()
|
||||
Interpreter.interpretFile(sys.argv[1], printAst=True, baseEnvironment=stdLibraryEnv)
|
||||
|
||||
except SmnpException as e:
|
||||
print(e.message())
|
||||
|
||||
@@ -9,12 +9,20 @@ from smnp.token.tokenizer import tokenize
|
||||
class Interpreter:
|
||||
|
||||
@staticmethod
|
||||
def interpretFile(file, printTokens=False, printAst=False):
|
||||
def interpretString(string, printTokens=False, printAst=False, baseEnvironment=None):
|
||||
return Interpreter._interpret(string.splitlines(), printTokens, printAst, baseEnvironment)
|
||||
|
||||
@staticmethod
|
||||
def interpretFile(file, printTokens=False, printAst=False, baseEnvironment=None):
|
||||
return Interpreter._interpret(readLines(file), printTokens, printAst, baseEnvironment)
|
||||
|
||||
@staticmethod
|
||||
def _interpret(lines, printTokens=False, printAst=False, baseEnvironment=None):
|
||||
environment = createEnvironment()
|
||||
if baseEnvironment is not None:
|
||||
environment.extend(baseEnvironment)
|
||||
|
||||
try:
|
||||
lines = readLines(file)
|
||||
|
||||
tokens = tokenize(lines)
|
||||
if printTokens:
|
||||
print(tokens)
|
||||
|
||||
Reference in New Issue
Block a user