Skip to content

Commit

Permalink
Feature Update
Browse files Browse the repository at this point in the history
Change for loop run time
change test
add time module
  • Loading branch information
iewnfod committed Sep 17, 2023
1 parent 9ea8936 commit ce11e84
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
6 changes: 6 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@
```
Reverse(s : STRING) RETURNS STRING
```
## [Time](./time.cpc)
* Time 获取当前时间戳
```
Time RETURNS REAL
```
3 changes: 3 additions & 0 deletions scripts/time.cpc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FUNCTION Time RETURNS REAL
RETURN PYTHON("from time import time; _result=time()")
ENDFUNCTION
6 changes: 3 additions & 3 deletions src/AST/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
28 changes: 25 additions & 3 deletions test/test.cpc
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ce11e84

Please sign in to comment.