feat(interpreter): add global time function
This commit is contained in:
3
examples/11_function_call.peb
Normal file
3
examples/11_function_call.peb
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
let t = time()
|
||||||
|
print("Time is")
|
||||||
|
print(t)
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Any, TYPE_CHECKING
|
from typing import Any, TYPE_CHECKING, Callable
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from src.interpreter.interpreter import Interpreter
|
from src.interpreter.interpreter import Interpreter
|
||||||
@@ -15,3 +15,16 @@ class PebbleCallable(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def call(self, interpreter: Interpreter, arguments: list[Any]) -> Any:
|
def call(self, interpreter: Interpreter, arguments: list[Any]) -> Any:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def make_builtin(func: Callable[[Interpreter, list[Any]], Any], nargs: int = 0) -> PebbleCallable:
|
||||||
|
class BuiltinFunction(PebbleCallable):
|
||||||
|
def arity(self) -> int:
|
||||||
|
return nargs
|
||||||
|
|
||||||
|
def call(self, interpreter: Interpreter, arguments: list[Any]) -> Any:
|
||||||
|
return func(interpreter, arguments)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"<native function>"
|
||||||
|
return BuiltinFunction()
|
||||||
|
|||||||
10
src/interpreter/globals.py
Normal file
10
src/interpreter/globals.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
from src.core.callable import make_builtin
|
||||||
|
from src.interpreter.environment import Environment
|
||||||
|
|
||||||
|
|
||||||
|
class GlobalEnvironment(Environment):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.define("time", make_builtin(lambda interpreter, args: time.time()))
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import time
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
from src.ast.expr import LiteralExpr, GroupingExpr, UnaryExpr, BinaryExpr, Expr, VariableExpr, AssignExpr, LogicalExpr, \
|
from src.ast.expr import LiteralExpr, GroupingExpr, UnaryExpr, BinaryExpr, Expr, VariableExpr, AssignExpr, LogicalExpr, \
|
||||||
@@ -6,16 +7,19 @@ from src.ast.stmt import Stmt, PrintStmt, ExpressionStmt, LetStmt, BlockStmt, If
|
|||||||
from src.core.callable import PebbleCallable
|
from src.core.callable import PebbleCallable
|
||||||
from src.interpreter.environment import Environment
|
from src.interpreter.environment import Environment
|
||||||
from src.interpreter.error import PebbleRuntimeError
|
from src.interpreter.error import PebbleRuntimeError
|
||||||
|
from src.interpreter.globals import GlobalEnvironment
|
||||||
from src.pebble import Pebble
|
from src.pebble import Pebble
|
||||||
from src.token import TokenType, Token
|
from src.token import TokenType, Token
|
||||||
|
|
||||||
|
|
||||||
class Interpreter(Expr.Visitor[Any], Stmt.Visitor[None]):
|
class Interpreter(Expr.Visitor[Any], Stmt.Visitor[None]):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.env: Environment = Environment()
|
self.global_env: GlobalEnvironment = GlobalEnvironment()
|
||||||
|
self.env: Environment = self.global_env
|
||||||
|
|
||||||
def interpret(self, statements: list[Stmt]) -> None:
|
def interpret(self, statements: list[Stmt]) -> None:
|
||||||
self.env.clear()
|
self.global_env = GlobalEnvironment()
|
||||||
|
self.env = self.global_env
|
||||||
try:
|
try:
|
||||||
for stmt in statements:
|
for stmt in statements:
|
||||||
self.execute(stmt)
|
self.execute(stmt)
|
||||||
|
|||||||
Reference in New Issue
Block a user