CAIE_Code
CAIE_Code copied to clipboard
An Interpreter for CAIE Pseudocode
CAIE Code (cpc)
the CAIE Pseudocode Interpreter
中文 | English
Installation and Update
Installation Preliminaries
- Have
python3
installed on your computer.
It is suggested to use
pypy3
to achieve the best efficiency.
- Have
git
installed on your computer. If you do not know whatgit
is, see https://git-scm.com/downloads.
For macOS users ensure you installed
Command Line Tools for Xcode
.
-
cargo
if you want to compile manually
Installation
- For macOS users, you can install directly using CAIE_Code_Installer.dmg from the releases page.
For other users...
-
Clone the project to your computer using
git clone https://github.com/iewnfod/CAIE_Code.git
. -
Enter the project folder:
cd CAIE_Code
. -
The executable programs are in
bin/
folder. You may directly run or consider addingbin/
to yourPATH
. -
If you want to compile manually:
-
macOS: run
build.sh
-
Windows: run
build.ps1
-
If you want to see the manual page from
man
command, you should consider link the manual fileman/cpc.1
to yourMANPATH
.For example(Linux):
sudo ln -f ./man/cpc.1 /your/man/path
. -
If you cannot execute the complied files, please submit the problems on our issue page.
Update
-
If you followed the instructions and used
git
or PKG to installcpc
, you can update easily usingcpc -u
. -
An auto-update feature is introduced after
dc0cd71
to automatically detect updates once a week. -
Otherwise, you should manually re-install the whole project.
Usage
cpc [filepath] [options]
It runs the entire file if filepath
is provided, otherwise, it enters playground mode.
Options
Mnemonic | Option | Description |
---|---|---|
-c |
--config |
To set configs of this interpreter |
-d |
--document |
To show the official document |
-h |
--help |
To show the help page |
-k |
--keywords |
To show all the keywords |
-m |
--migrate |
To migrate .p files to .cpc in a specified directory |
-n |
--notification |
To show notification published by developer (only if this is not expired) |
-p |
--parse |
To show parse information during running |
-t |
--time |
To show the time for the script to run |
-u |
--update |
To update the version |
-v |
--version |
To show the version of this interpreter |
-gt |
--get-tree |
To show the tree of the program after being parsed |
-lc |
--list-configs |
To list all the configs of the interpreter |
-ne |
--no-error |
To remove all error messages |
-rc |
--reset-configs |
To reset all the configs of the interpreter |
-init |
--init-requirements |
To install all dependences |
Config
-
remote
-
github
: Use GitHub as the update source. This source is always the latest. -
gitee
: Use Gitee as the update source, which might be slower than Github.
-
-
branch
-
stable
: Updates are slow, but the most stable. -
nightly
: This branch will update once a day. Early adopters can try this branch, please actively report the issue. -
dev
: The latest version of CPC may contain many untested functions.
This setting needs to be run
cpc -u
once for it to take effect.In a developer mod, your remote will not be changed by config and the branch will be locked in
dev
. -
-
auto-update
-
true
:Enable auto update. -
false
:Disable auto update.
-
-
last-auto-update
All non-negative real numbers are accepted and automatically updated by the system. -
interval-update
All non-negative integers, in seconds, are accepted as the automatic update interval. -
recursion-limit(rl)
all integer number as the recursion depth limit of the interpreter. -
integrity-protection
-
true
: Enable integrity protection. -
false
: Disable integrity protection.
Integrity Protection prevent any accidental or malicious modification of the interpreter.
This protection will be automatically disabled in developer mode.
-
-
dev
-
true
: Enable developer mode. -
false
: Disable developer mode.
-
-
Developer Options
-
dev.simulate-update
-
true
: Enable simulation updates -
false
: Disable simulation updates
-
-
FAQs
Import Error
Please try to install all Python packages manually using
pip install -r requirements.txt
Import Error after I manually installed packages
The program will automatically detect pypy3
when available and use it to interpret pseudocode because it has better efficiency.
If you have pypy3
installed you should install manually using pypy3 -m pip install -r requirements.txt
Incorrect <up>, <down>, <left>, <right> key actions when in playground mode
You should retry pip install readline
or pip install gnureadline
.
OSError when launching
Enter the directory of this project, and run
rm -rf .cpc_history
cpc -u
Other problems?
If it still fails after re-installation, please report it to us on the issue page.
Efficiency test
Test Environment
- machine: 2020 MacBook Pro
- processor: Apple M1
- RAM: 8GB
- Cores: 8 (4 efficient, 4 performance)
- OS: macOS 14.1.1 (23B81)
- Python version: PyPy 3.9.16
Basic Tests
- assignment: 1200w/s
DECLARE a : INTEGER
FOR i <- 1 TO 12000000
a <- i
NEXT i
- explicit conversion and assignment: 760w/s
DECLARE a : STRING
FOR i <- 1 TO 7600000
a <- STRING(i)
NEXT i
- implicit conversion and assignment: 1000w/s
DECLARE a : STRING
FOR i <- 1 TO 10000000
a <- i
NEXT i
- print to terminal: 65w/s
FOR i <- 1 TO 650000
OUTPUT i
NEXT i
Computation Tests
- generating 100k randoms and shell sorting: about 3.5s
Standards
Basic Standards
- use camelCase naming
- use
.cpc
as the file suffix - use
utf-8
encoding - all reserved words are in upper case
- the program is case-sensitive
- use
//
to comment code
Features
Most syntax follows the pseudocode standard of CAIE.
However, indentation is suggested but not compulsory.
Each statement following CASE
statement must end with a ;
, semicolon.
Basic Data Types
The following items give the DATATYPE
, its description, and the default value set when defined in brackets.
-
INTEGER
integer, whole number (0
)1 2 123 -123
-
REAL
float-point number (0.0
)1.1 0.1 -12.1233
-
CHAR
one single character (''
)'1' '!' 'd'
-
STRING
string (""
)"Hello" "World" "!"
-
BOOLEAN
boolean (FALSE
)TRUE FALSE
-
DATE
date (current date)25/07/2023 10/12/2012
None
is a null datatype returned by some special functions. it should not be used in normal coding and cannot be declared.
Syntax Definitions
-
Variables and constants
- Declare variables
DECLARE <identifier> : <data type> DECLARE <identifier> : ARRAY [<lower>:<upper>, ...] OF <data type>
- Declare constants
CONSTANT <identifier> = <value>
- Assignment
<identifier> <- <value> <identifier>[<index>, ...] <- <value>
Pseudocode uses
<-
instead of=
.
- Declare variables
-
I/O
- Input
INPUT <identifier>
- Output
OUTPUT <value>, ...
- Input
-
Operations
-
+
addition -
-
subtraction -
*
multiplication -
/
division -
>
greater than -
>=
greater than or equal to -
<
smaller than -
<=
smaller than or equal to -
=
equal (NOT AN ASSIGNMENT STATEMENT) -
<>
not equal -
&
conglomerate strings -
MOD
modulus, find the remainder -
DIV
integer division
-
-
Logic operations
-
AND
-
OR
-
NOT
-
-
Conditional statements
- IF statements
IF <condition> THEN <statements> ENDIF IF <condition> THEN <statements> ELSE <statements> ENDIF
- CASE statements
IMPORTANT: official standards do not have semicolons
;
hereCASE OF <identifier> <value> : <statements>; <value> : <statements>; ... OTHERWISE : <statements>; ENDCASE
- IF statements
-
Loop statements
- FOR loop
FOR <identifier> <- <value> TO <value> <statements> NEXT <identifier>
- REPEAT loop
REPEAT <statements> UNTIL <condition>
- WHILE loop
WHILE <condition> <statements> ENDWHILE
- FOR loop
-
functions
- functions without a return value (procedure)
PROCEDURE <identifier> () <statements> ENDPROCEDURE PROCEDURE <identifier> (<param> : <data type>, ...) <statements> ENDPROCEDURE
- call a procedure
CALL <identifier> () CALL <identifier> (<value>, ...)
- functions with return values
FUNCTION <identifier> RETURNS <data type> <statements> RETURN <value> ENDFUNCTION FUNCTION <identifier> (<param> : <data type>, ...) RETURNS <data type> <statements> RETURN <value> ENDFUNCTION
- call a function with return values
<identifier> () <identifier> (<value>, ...)
- Before the parameters of those sub-routines, you can use
BYREF
orBYVAL
to force the program to pass those parameters by reference or by-value respectively. If noBYREF
norBYVAL
is given, the program will follow the prior parameter. If the program cannot find a clear indication it will, by default pass parameters by value.- If you explicitly define the data types of the array passed
BYVAL
the program will implicitly convert to the designated data type; the program will not convert data types when passedBYREF
. -
BYREF
: pass the reference of a variable -
BYVAL
: pass a copy of the variable
- If you explicitly define the data types of the array passed
- functions without a return value (procedure)
-
File I/O
- open a file
OPENFILE <file path> FOR <file mode>
- read a file
READFILE <file path>, <variable>
- write to a file
WRITEFILE <file path>, <data>
- close a file
CLOSEFILE <file path>
- locate in the file
SEEK <file path>, <address>
- File Mode
-
READ
-
WRITE
-
APPEND
-
RANDOM
-
- open a file
-
Self-defined data types
- enumerate type
TYPE <identifier> = (<identifier>, ...)
- pointer type
TYPE <identifier> = ^<data type>
- records(classes)
In this case, the program will not check the data types when assigning a variable of this type to another. The program will assign the other variable as the reference for this one.
TYPE <identifier> <statements> ENDTYPE
- enumerate type
-
Object Oriented Programme
- define an object
CLASS <identifier> PUBLIC PROCEDURE NEW (<params>) <statements> ENDPROCEDURE <statements> ENDCLASS
- private or public variable
PRIVATE <identifier> : <type> PUBLIC <identifier> : <type>
- private or public procedure and function
PRIVATE PROCEDURE <identifier> (<params>) <statements> ENDPROCEDURE PUBLIC PROCEDURE <identifier> (<params>) <statements> ENDPROCEDURE PRIVATE FUNCTION <identifier> (<params>) RETURNS <type> <statements> ENDFUNCTION PUBLIC FUNCTION <identifier> (<params>) RETURNS <type> <statements> ENDFUNCTION
- create an object
NEW <identifier> (<values>)
If you do not sign a variable or procedure or function explicitly, it will be public by default.
- define an object
Special Syntax of CPC Interpreter
- delete a variable or constant on RAM
DELETE <identifier>
- do nothing
PASS
- import CPC files
IMPORT <expression>
expression
here should be a string within double quotes. There is no isolation between the imported file and the main file. Identifiers may collide. It is suggested to use theImport
function to import a package instead.CONSTANT <identifier> = Import("<path to import file>")
Built-in Functions from CAIE Standard
-
LEFT(ThisString : STRING, x : INTEGER) RETURNS STRING
$ LEFT("ABCDEFGH", 3) "ABC"
-
RIGHT(ThisString : STRING, x : INTEGER) RETURNS STRING
$ RIGHT("ABCDEFGH", 3) "FGH"
-
LENGTH(ThisString : STRING) RETURNS INTEGER
$ LENGTH("Happy Days") 10
-
MID(ThisString : STRING, x : INTEGER, y : INTEGER) RETURNS STRING
$ MID("ABCDEFGH", 2, 3) "BCD"
-
LCASE(ThisChar : CHAR) RETURNS CHAR
Decrapricated since 2023
$ LCASE('W') 'w'
-
UCASE(ThisChar : CHAR) RETURNS CHAR
Decrapricated since 2023
$ UCASE('h') 'H'
-
TO_UPPER(x : <datatype>) RETURNS <datatype>
may be CHAR
orSTRING
$ TO_UPPER("hello")
"HELLO"
$ TO_UPPER('a')
'A'
-
TO_LOWER(x : <datatype>) RETURNS <datatype>
may be CHAR
orSTRING
$ TO_LOWER("HELLO")
"hello"
$ TO_LOWER('A')
'a'
-
INT(x : REAL) RETURNS INTEGER
$ INT(27.5415) 27
-
RAND(x : INTEGER) RETURNS REAL
$ RAND(87) 35.43
-
EOF(file_path : STRING) RETURNS BOOLEAN
-
POW(x: REAL, y: REAL) RETURNS REAL
-
DAY(ThisDate : DATE) RETURNS INTEGER
$ DAY(25/07/2023) 25
-
MONTH(ThisDate : DATE) RETURNS INTEGER
$ MONTH(25/07/2023) 7
-
YEAR(ThisDate : DATE) RETURNS INTEGER
$ YEAR(12/12/2005) 2005
-
DAYINDEX(ThisDate : DATE) RETURNS INTEGER
Where Sunday = 1, Monday = 2 etc
$ DAYINDEX(25/03/2024)
2
-
SETDATE(day : INTEGER, month : INTEGER, year : INTEGER) RETURNS DATE
$ SETDATE(25, 03, 2024) 25/03/2024
-
TODAY() RETURNS DATE
$ TODAY() 25/03/2024
Built-in Functions of this Interpreter
These functions are fairly useful, but they are not included in the CAIE standard.
-
EXIT(code : INTEGER)
: exit the program with the exit codecode
. Defalt exit code is 0. -
ROUND(x : REAL, decimalPlace : INTEGER)
: round the float-point numberx
to some decimal place. The default decimal place is 0(to the nearest whole number). -
PYTHON(code : STRING, *args)
is a Python interface. You can pass any Python statements intocode
and the program will run it in standard Python. the return value of this function is the value of variable_result
in the Python code.Example:
> DECLARE a : INTEGER > a <- 0 > OUTPUT PYTHON("_result=a+1", a) 1
if the Python code does not assign a value to
_result
, the function will returnNone
. you must pass all variables used in the Python code in*args
, otherwise, it will not run correctly. -
VARTYPE(v)
is an interface to get the type ofv
and return it as a string. -
ANY
is a type that used to allow some unknown type data. -
For more non-official scripts, please see scripts.
Targets
Version 0.1.x Target
- [ ] Implement all features provided by official document.
- [ ] Increase the stability for running to achieve a relatively useable situation.
Version 0.2.x Target
- [ ] Give the kernel a great update and optimization.
- [ ] Implement a high performance virtual machine to run. (Similar as the solution of Java)
Version 0.3.x Target
- [ ] Allow building into executable binary file.
Long-term Targets
- [ ] Provide more packages for use.
- [ ] Increase running speed and stability.
- [ ] Implement bootstrap.