Skip to content

Commit

Permalink
Add recursive limit option
Browse files Browse the repository at this point in the history
  • Loading branch information
iewnfod committed Sep 17, 2023
1 parent ce11e84 commit d7e423d
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ cpc [file_path] [options]
| `-v` | `--version` | To show the version of this interpreter |
| `-ne` | `--no-error` | To remove all error messages |
| `-u` | `--update` | To update the version (only useful when using a version equal or greater than `0.1.2` and installed by git) |
| `-r` | `--recursive-limit` | To set the recursive limit of the interpreter |

### 常见问题
#### 出现 `Import Error`
Expand Down
15 changes: 11 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,23 @@ def wrong_argument(msg):
def main():
# 解析参数
file_path = ''
for arg in argv[1:]:
i = 1
while i < len(argv):
arg = argv[i]
for opt in options.arguments:
if opt.check(arg):
opt.run()
opt.run(argv[i:])
i += opt.value_num
break
else:
if arg[0] == '-':
wrong_argument(f'Unknown option {arg}')
wrong_argument(f'Unknown option `{arg}`')
else:
file_path = arg
if file_path == '':
file_path = arg
else:
wrong_argument(f'There should only be one file path, but found `{file_path}` and `{arg}`')
i += 1

# 预加载文件
preload_scripts()
Expand Down
6 changes: 5 additions & 1 deletion man/cpc.1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
. ftr VB CB
. ftr VBI CBI
.\}
.TH "CPC" "1" "September 8, 2023" "cpc 0.1.2" "User Manual"
.TH "CPC" "1" "September 8, 2023" "cpc 0.1.3" "User Manual"
.hy
.SH NAME
.PP
Expand Down Expand Up @@ -53,6 +53,10 @@ To remove all error messages
\f[B]\[en]parse\f[R]
To show parse information during running
.TP
\f[B]-r\f[R]
\f[B]\[en]recursive-limit\f[R]
To set the recursive limit of the interpreter
.TP
\f[B]-t\f[R]
\f[B]\[en]time\f[R]
To show the time for the script to run
Expand Down
6 changes: 5 additions & 1 deletion man/cpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: CPC
section: 1
header: User Manual
footer: cpc 0.1.2
footer: cpc 0.1.3
date: September 8, 2023
---

Expand Down Expand Up @@ -36,6 +36,10 @@ cpc - An interpreter for CAIE Pseudocode.
: **--parse**
: To show parse information during running

**-r**
: **--recursive-limit**
: To set the recursive limit of the interpreter

**-t**
: **--time**
: To show the time for the script to run
Expand Down
15 changes: 10 additions & 5 deletions src/options.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sys import exit
from sys import exit, setrecursionlimit
import platform
from .update import VERSION, update

Expand Down Expand Up @@ -74,23 +74,27 @@ def remove_error():
def update_version():
update()

def set_recursive_limit(v):
setrecursionlimit(int(v))

# 输入参数: (参数简写, 参数全称, 运行函数, 描述, 是否需要退出, 函数所需参数)

# 输入参数: (参数简写, 参数全称, 运行函数, 描述, 是否需要退出, 是否需要参数,参数数量,函数所需参数)
class Opt:
def __init__(self, short_arg, long_arg, func, description, exit_program, *args, **kwargs):
def __init__(self, short_arg, long_arg, func, description, exit_program, value_num=0, *args, **kwargs):
self.short_arg = short_arg
self.long_arg = long_arg
self.func = func
self.description = description
self.exit_program = exit_program
self.value_num = value_num
self.args = args
self.kwargs = kwargs

def check(self, t):
return t == self.short_arg or t == self.long_arg

def run(self):
self.func(*self.args, **self.kwargs)
def run(self, value):
self.func(*value[1:self.value_num+1], *self.args, **self.kwargs)
if self.exit_program:
quit(0)

Expand All @@ -107,4 +111,5 @@ def __lt__(self, other):
Opt('-k', '--keywords', show_keywords, 'To show all the keywords', True),
Opt('-ne', '--no-error', remove_error, 'To remove all error messages', False),
Opt('-u', '--update', update_version, 'To check or update the version (only if this is installed with git)', True),
Opt('-r', '--recursive-limit', set_recursive_limit, 'To set the recursive limit of the interpreter', False, 1)
]
5 changes: 2 additions & 3 deletions src/quit.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# from os import _exit
from os import _exit
from .global_var import output_error, console
from .AST import stack

def quit(code=0):
output_error()
console.postloop()
stack.delete()
exit(code)
# _exit(code)
_exit(code)
30 changes: 30 additions & 0 deletions test/recursive_test.cpc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
DECLARE test_num : INTEGER
DECLARE t : REAL

test_num <- 370

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

t <- Time()
OUTPUT "aux: "
OUTPUT aux(test_num+1, 0, 1)
OUTPUT Time() - t


DECLARE arr : ARRAY[1:test_num] OF INTEGER
FUNCTION fib(i:INTEGER) RETURNS INTEGER
IF i = 1 THEN RETURN 1 ENDIF
IF i = 2 THEN RETURN 1 ENDIF
IF arr[i] <> 0 THEN RETURN arr[i] ENDIF
arr[i] <- fib(i - 1) + fib(i - 2)
RETURN arr[i]
ENDFUNCTION

t <- Time()
OUTPUT "fib: "
OUTPUT fib(test_num)
OUTPUT Time() - t
28 changes: 3 additions & 25 deletions test/test.cpc
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
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
DECLARE a : INTEGER
a <- 1
OUTPUT a

0 comments on commit d7e423d

Please sign in to comment.