From 50e838b4bb0f2931e00ad886f776a29861a04e22 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 6 Feb 2026 13:23:36 +0100 Subject: [PATCH] feat(interpreter): add global time function --- examples/11_function_call.peb | 3 +++ src/core/callable.py | 15 ++++++++++++++- src/interpreter/globals.py | 10 ++++++++++ src/interpreter/interpreter.py | 8 ++++++-- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 examples/11_function_call.peb create mode 100644 src/interpreter/globals.py diff --git a/examples/11_function_call.peb b/examples/11_function_call.peb new file mode 100644 index 0000000..58041d5 --- /dev/null +++ b/examples/11_function_call.peb @@ -0,0 +1,3 @@ +let t = time() +print("Time is") +print(t) \ No newline at end of file diff --git a/src/core/callable.py b/src/core/callable.py index 99d0770..73cc44e 100644 --- a/src/core/callable.py +++ b/src/core/callable.py @@ -1,7 +1,7 @@ from __future__ import annotations from abc import ABC, abstractmethod -from typing import Any, TYPE_CHECKING +from typing import Any, TYPE_CHECKING, Callable if TYPE_CHECKING: from src.interpreter.interpreter import Interpreter @@ -15,3 +15,16 @@ class PebbleCallable(ABC): @abstractmethod 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"" + return BuiltinFunction() diff --git a/src/interpreter/globals.py b/src/interpreter/globals.py new file mode 100644 index 0000000..ed6d6fd --- /dev/null +++ b/src/interpreter/globals.py @@ -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())) diff --git a/src/interpreter/interpreter.py b/src/interpreter/interpreter.py index 96dd95d..808e191 100644 --- a/src/interpreter/interpreter.py +++ b/src/interpreter/interpreter.py @@ -1,3 +1,4 @@ +import time from typing import Any, Optional 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.interpreter.environment import Environment from src.interpreter.error import PebbleRuntimeError +from src.interpreter.globals import GlobalEnvironment from src.pebble import Pebble from src.token import TokenType, Token class Interpreter(Expr.Visitor[Any], Stmt.Visitor[None]): 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: - self.env.clear() + self.global_env = GlobalEnvironment() + self.env = self.global_env try: for stmt in statements: self.execute(stmt)