mirror of
https://github.com/Alvin-Zilverstand/femcode.git
synced 2026-03-06 11:06:47 +01:00
feat: Implement built-in functions (len, type)
This commit is contained in:
10
examples/built_ins.fem
Normal file
10
examples/built_ins.fem
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
my_string is "Hello"
|
||||||
|
my_list is [1, 2, 3]
|
||||||
|
|
||||||
|
UwU Boy len(my_string)
|
||||||
|
UwU Boy len(my_list)
|
||||||
|
|
||||||
|
UwU Boy type(my_string)
|
||||||
|
UwU Boy type(my_list)
|
||||||
|
UwU Boy type(123)
|
||||||
|
UwU Boy type(Kawaii)
|
||||||
@@ -3,7 +3,9 @@ class Interpreter:
|
|||||||
self.ast = ast
|
self.ast = ast
|
||||||
self.scope_stack = [{}]
|
self.scope_stack = [{}]
|
||||||
self.functions = {
|
self.functions = {
|
||||||
"ask": self._ask_builtin
|
"ask": self._ask_builtin,
|
||||||
|
"len": self._len_builtin,
|
||||||
|
"type": self._type_builtin
|
||||||
}
|
}
|
||||||
|
|
||||||
def _ask_builtin(self, prompt):
|
def _ask_builtin(self, prompt):
|
||||||
@@ -174,6 +176,14 @@ class Interpreter:
|
|||||||
def visit_ReturnStatement(self, node):
|
def visit_ReturnStatement(self, node):
|
||||||
raise ReturnValue(self.visit(node.value))
|
raise ReturnValue(self.visit(node.value))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _len_builtin(obj):
|
||||||
|
return len(obj)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _type_builtin(obj):
|
||||||
|
return str(type(obj).__name__)
|
||||||
|
|
||||||
class ReturnValue(Exception):
|
class ReturnValue(Exception):
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
self.value = value
|
self.value = value
|
||||||
Reference in New Issue
Block a user