Create standard library proof of concept
This commit is contained in:
@@ -1,12 +1,7 @@
|
|||||||
from smnp.environment.environment import Environment
|
from smnp.environment.environment import Environment
|
||||||
from smnp.module import functions, methods
|
from smnp.module import functions, methods
|
||||||
from smnp.type.model import Type
|
|
||||||
|
|
||||||
|
|
||||||
def createEnvironment():
|
def createEnvironment():
|
||||||
variables = {
|
return Environment([{}], functions, methods)
|
||||||
"bpm": Type.integer(120)
|
|
||||||
}
|
|
||||||
|
|
||||||
return Environment([ variables ], 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
|
import sys
|
||||||
|
|
||||||
from smnp.error.base import SmnpException
|
from smnp.error.base import SmnpException
|
||||||
|
from smnp.library.loader import loadStandardLibrary
|
||||||
from smnp.program.interpreter import Interpreter
|
from smnp.program.interpreter import Interpreter
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
|
stdLibraryEnv = loadStandardLibrary()
|
||||||
Interpreter.interpretFile(sys.argv[1], printAst=True)
|
Interpreter.interpretFile(sys.argv[1], printAst=True, baseEnvironment=stdLibraryEnv)
|
||||||
|
|
||||||
except SmnpException as e:
|
except SmnpException as e:
|
||||||
print(e.message())
|
print(e.message())
|
||||||
|
|||||||
@@ -9,12 +9,20 @@ from smnp.token.tokenizer import tokenize
|
|||||||
class Interpreter:
|
class Interpreter:
|
||||||
|
|
||||||
@staticmethod
|
@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()
|
environment = createEnvironment()
|
||||||
|
if baseEnvironment is not None:
|
||||||
|
environment.extend(baseEnvironment)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
lines = readLines(file)
|
|
||||||
|
|
||||||
tokens = tokenize(lines)
|
tokens = tokenize(lines)
|
||||||
if printTokens:
|
if printTokens:
|
||||||
print(tokens)
|
print(tokens)
|
||||||
|
|||||||
Reference in New Issue
Block a user