From 6e9e252b8659f00ff8b18effd94bed23cf2acebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Mon, 15 Jul 2019 23:54:21 +0200 Subject: [PATCH] Create 'read' function --- smnp/module/system/__init__.py | 4 +- smnp/module/system/function/read.py | 69 +++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/smnp/module/system/__init__.py b/smnp/module/system/__init__.py index ced0515..28d9297 100644 --- a/smnp/module/system/__init__.py +++ b/smnp/module/system/__init__.py @@ -1,4 +1,4 @@ -from smnp.module.system.function import sleep, display, displayln, debug, exit, type +from smnp.module.system.function import sleep, display, displayln, debug, exit, type, read -functions = [ debug.function, display.function, displayln.function, exit.function, sleep.function, type.function ] +functions = [ debug.function, display.function, displayln.function, exit.function, sleep.function, type.function, read.function ] methods = [] \ No newline at end of file diff --git a/smnp/module/system/function/read.py b/smnp/module/system/function/read.py index e24877f..07d67f9 100644 --- a/smnp/module/system/function/read.py +++ b/smnp/module/system/function/read.py @@ -1,3 +1,72 @@ +from smnp.error.runtime import RuntimeException +from smnp.function.model import CombinedFunction, Function +from smnp.function.signature import signature +from smnp.token.tokenizers.bool import boolTokenizer +from smnp.token.tokenizers.note import noteTokenizer +from smnp.type.model import Type +from smnp.type.signature.matcher.type import ofType + +_signature1 = signature() +def _function1(env): + value = input() + return Type.string(value) + + +_signature2 = signature(ofType(Type.STRING)) +def _function2(env, prompt): + print(prompt.value, end="") + value = input() + return Type.string(value) + + +_signature3 = signature(ofType(Type.TYPE)) +def _function3(env, type): + value = input() + return getValueAccordingToType(value, type) + + +def getValueAccordingToType(value, type): + try: + if type.value == Type.STRING: + return Type.string(value) + + if type.value == Type.INTEGER: + return Type.integer(int(value)) + + if type.value == Type.BOOL: + consumedChars, token = boolTokenizer(value, 0, 0) + if consumedChars > 0: + return Type.bool(token.value) + + return ValueError() + + if type.value == Type.NOTE: + consumedChars, token = noteTokenizer(value, 0, 0) + if consumedChars > 0: + return Type.note(token.value) + + raise ValueError() + + raise RuntimeException(f"Type {type.value.name.lower()} is not suuported", None) + + except ValueError: + raise RuntimeException(f"Invalid value '{value}' for type {type.value.name.lower()}", None) + + +_signature4 = signature(ofType(Type.STRING), ofType(Type.TYPE)) +def _function4(env, prompt, type): + print(prompt.value, end="") + value = input() + return getValueAccordingToType(value, type) + + +function = CombinedFunction( + 'read', + Function(_signature1, _function1), + Function(_signature2, _function2), + Function(_signature3, _function3), + Function(_signature4, _function4) +) # TODO read function # def read(args, env):