diff --git a/scripts/README.md b/scripts/README.md index d81ae1c..478a58e 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -58,3 +58,9 @@ ``` Reverse(s : STRING) RETURNS STRING ``` + +## [Time](./time.cpc) +* Time 获取当前时间戳 + ``` + Time RETURNS REAL + ``` diff --git a/scripts/time.cpc b/scripts/time.cpc new file mode 100644 index 0000000..76482cf --- /dev/null +++ b/scripts/time.cpc @@ -0,0 +1,3 @@ +FUNCTION Time RETURNS REAL + RETURN PYTHON("from time import time; _result=time()") +ENDFUNCTION diff --git a/src/AST/program.py b/src/AST/program.py index 76dbc51..86a0fee 100644 --- a/src/AST/program.py +++ b/src/AST/program.py @@ -87,14 +87,14 @@ def exe(self): else: diff = 1 - # 创建 index 变量 - stack.new_variable(self.id, 'INTEGER') - # 核对id是否相同 if self.id != self.next_id: print(f'Expect `{self.id}` for next id, but found `{self.next_id}`') return + # 创建 index 变量 + stack.new_variable(self.id, 'INTEGER') + for i in range(left[0], right[0]+diff, step[0]): # 给 index 赋值 stack.set_variable(self.id, i, 'INTEGER') diff --git a/test/test.cpc b/test/test.cpc index e8290cd..20b5d93 100644 --- a/test/test.cpc +++ b/test/test.cpc @@ -1,3 +1,25 @@ -DECLARE a : INTEGER -OUTPUT a -DELETE a +DECLARE t1 : REAL +DECLARE t2 : REAL + +FUNCTION aux(n:INTEGER, acc1:INTEGER, acc2:INTEGER) RETURNS INTEGER +    IF n = 1 THEN RETURN acc1 ENDIF +    IF n = 2 THEN RETURN acc2 ENDIF +    RETURN aux(n - 1, acc2, acc1 + acc2) +ENDFUNCTION + +t1 <- PYTHON("from time import time; _result=time()") +OUTPUT "aux: " +OUTPUT aux(46, 0, 1) +OUTPUT PYTHON("from time import time; _result=time()") - t1 + + +FUNCTION fib(i:INTEGER) RETURNS INTEGER + IF i = 1 THEN RETURN 1 ENDIF + IF i = 2 THEN RETURN 1 ENDIF + RETURN fib(i - 1) + fib(i - 2) +ENDFUNCTION + +t2 <- PYTHON("from time import time; _result=time()") +OUTPUT "fib: " +OUTPUT fib(46) +OUTPUT PYTHON("from time import time; _result=time()") - t2