https://github.com/user19870/cppsp
cppsp --a transpiled script-like language base on c++
https://github.com/user19870/cppsp
compile-language cpp cppsp linux mac open-source script-like transpiled-language windows
Last synced: 4 days ago
JSON representation
cppsp --a transpiled script-like language base on c++
- Host: GitHub
- URL: https://github.com/user19870/cppsp
- Owner: user19870
- License: gpl-3.0
- Created: 2025-12-06T16:00:19.000Z (4 months ago)
- Default Branch: First
- Last Pushed: 2026-03-22T15:10:43.000Z (14 days ago)
- Last Synced: 2026-03-23T00:28:59.899Z (14 days ago)
- Topics: compile-language, cpp, cppsp, linux, mac, open-source, script-like, transpiled-language, windows
- Language: C++
- Homepage:
- Size: 14 MB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cppsp
cppsp -a transpiled script-like language base on c++
* [English](https://github.com/user19870/cppsp)
* [中文](.github/workflows/README_tw.md)
## Install
Download the cppsp_compiler.exe or compiler the sourcecode by yourself
* Requirement:prepare your own c++ compiler and set it's folder to environment path(environment variable)
* Requirement:a 64bits c++ compiler to make sure exe can be open
* Optional: put the folder path of exe/elf/mach-o to environment
* remember delete _mac/_linux to make cppsp_compiler works
* (Optional) rename cppsp_compiler.exe(or cppsp_compiler) to any name you like to change compile command like:cppsp、abcdef....
* Install from cmd/console:
#### Windows:
```
curl -L -o cppsp_compiler.exe https://github.com/user19870/cppsp/raw/refs/heads/First/cppsp_compiler.exe
```
#### Linux:
```
curl -L -o cppsp_compiler https://github.com/user19870/cppsp/raw/refs/heads/First/cppsp_compiler_linux.delete_linux
```
#### Mac:
```
curl -L -o cppsp_compiler https://github.com/user19870/cppsp/raw/refs/heads/First/cppsp_compiler_mac.delete_mac
```
## Usage
* Use cmd or other console to compiler .cppsp file:
`cppsp_compiler(if not in environment path:.\cppsp_compiler.exe or c:\...\cppsp_compiler.exe) script.cppsp`
* **`cppsp_compiler mod.cppsp -header`** will generate .h file and turn int main(){...} a comment
* **`cppsp_compiler new project`** : create new project with empty include.ini、lib.ini、module.ini、project.cppsp and setting current path in the three .ini
* Setting c++ include/lib/cppsp_mod folder by .ini file
```
include.ini:C:\...\include1,c:\...\include2
lib.ini:C:\...\lib1,c:\...\lib2
module.ini:C:\...\modfolder1,c:\...\modfolder2
```
## Feature
* can compile when there is only print("hello world") in .cppsp
* can use almost c++ header by import
- can use .cppsp mod by import
- deepermod.cppsp still can use c++ header and .cppsp mod by import
* user-custom syntaxs by pattern-driven, nested, namespace-scoped templates
* can use c++ code by @inject and @function
* enable indentation and multi-line after v1.3
* declare multi variables with `var`.....`type`
* control variables inside keyword or globle
* Can use utf8 encoding bytes as alternative of filename: `cppsp_compiler \xe9\x80\x99\xe6\x98\xaf\x20\xe4\xb8\xad\xe6\x96\x87\x68\x75\x20\x6b\x6f\x6c\x20\x20\x70\xe6\xaa\x94\xe6\xa1\x88\x2e\x63\x70\x70\x73\x70`
* Enable compile filename encoded by utf8(I try my best but some devices may not work.)
## Keyword
* `#useclang` or `#usegcc` : use clang++ or g++ compile command
* `@command("...")`: add command when compile like:-Os、-m64
* `#overwrite`:make `@command()` overwrite g++ .... or clang++ compile command like `@command("g++ -Os -m64 -nostdlib -shared -o dll.dll dll.cpp")` and add "*/" after int main(){..} also add "/\*" in front of int main(){..}
* `#skipcompile` : skip g++/clang++ compile and directly run output
- `import` :import header in c++ and accept import iostream,cstdio,x,y,.....
- import can also import .cppsp mods like`import a.b.mod`| **a.b.mod represent the path a/b/mod.cppsp and path will be searched from parent path in module.ini** | a.b.mod also generate namespace a{ namespace b{ namespace mod{...}}}
* `package` : it is written in .cppsp, `package d.e.f` will replace namespace generated by import a.b.c
* `@function<<...>>`: inject everything(void()、int()、bool()、even #define and using namespace) in <<...>> to the space under #include above int main().**Better not put any word, symbol or blanks but enalbe "enter" or nothing after ">>" to make difference between operation like `a>>b` and <<....>>**
* `@inject(...)` :inject everything in (...) to int main(){...} ----`@inject(....)`and `@inject("....")` are both legal. @inject(....) belonging to line ast and @inject("....") belonging to token ast but @inject(....) will be transpiled before @inject("....") and any keyword belonging to token ast.)
* `print()`: print content to console like print("12\n"," ",1," ",2.1,true,false," ")
* `input()`: input data to variables
* `var`.....`type`: declare variables with/without values. Support multi variables and type can be written as `int/float/char/string/bool`. <{1+1}> is a value but 1+1 not(it's expression)
* `if/else/else if/while(...){...}`:similar to "if/else/else if" in c++ but enable syntax: `if(input(x)>1)`. Can write cppsp keywords and operation( =,+,-,*,/,++....) in {...}
* `for(...){...}`:similae to "for" in c++ like for( type i=0,i<10,i++), for(type i=0,j=10;i<10&&j>0;i++,j++) or for(type i:x) . Can write cppsp keywords and operation( =,+,-,*,/,++....) in {...}
* `function f()...`:`function f() type {...return...}` will define a function with type, `function f(){...}` will define a void function,`function f()` will declare a void function, `function f(){...return...}` will define a function with auto in c++. [Usages](example/functionTest.cppsp)
```
function [std::pow,std::sort,abs,sqrt] // will regist functions from c++
// but template function still need <{...}> like std::sort(x,x+5,<{ std::greater()}>)
```
[Usecppfunction](example/Usecppfunction.cppsp)
* [`struct S{...}`](example/structtest.cppsp) : define a structure and the name of structure will become a type ,so can use something like var....S. If write `struct a b c`without `{}` a ,b, and c will become type but won't generate any c++ code
- [`@custom xxx("...",<{...}>,...)`](https://github.com/user19870/cppsp/blob/First/example/customSyntax.cppsp) : @custom can let users write own syntaxs. it is a transpile-time pattern-driven code generator with nested templates, namespace-scoped features **"..." can generate code, <{...}> is similar to it but will become a placeholder and replaced by parameter when the custom syntax is called.** Code will generate in global and the inner of some cppsp keywords. ` namespace n{ @custom.... }`
- If there is any "@" is in @custom like `@custom vec@mn("std::vector<",<{type}>,">") ` vec@mn(...) will generate code in main(){....}
```
@custom subs(<{T}>," sub(",<{T a}>,",",<{T b}>,")"," {return a-b;}")
subs(int ,int a,int b)
```
- `use` : use namespaces like :`use a.b.c`. "xxx" from @custom xxx(...) also affected by `use`
- `use a.b.c->d` can use function/struct/other in namespace a.b.c but not use the whole namespace
* `//`:comment
## Syntax
* control variables in cppsp once a line or separate by `;`
```
y=e()
y=4.6; y++ ;y++
```
* `<{...}>` : c++ code or everything in <{...}> will become an element of cppsp keyowds like :
```
import math.h,iostream
print( <{pow(2,3)}>)
```
* `var`......`type`:
```
import string,iostream
var a,c,d = 1,
<{(2*2+6)/2}>
,4 int
var b = "hello world" string
var f1,f2,f3 float
var c1 char
var b1 = <{1+1==2}> bool
input(f1)
print(a," ",c," ",d," ",b," ",b1," ",f1)
```
* array and operation:
```
import iostream
var x={1,2,3} int
@inject("x[0]=3;x[1]=2; x[2]=1;")
if(true) {
x[0]=4
}
for(int i=0,i<3,i++) {x[i]=0}
```
## OOP
* Inheritance : enable single and multi inheritance, use `public: a, b,c` inheritance in c++
```
struct local{
extension_slot("local")
}
struct der derive(cppsp.test.mytype,local,<{std::vector}>){
}
```
* Extension : used to extend a struct. extension_slot("id") can provide a slot to expand(id is decided by mod writter)
```
import cppsp.cpp17.base, test
struct local{
private:
function f1(){}
var v int
public:
function f2(){}
var outv int
extension_slot("local")
}
struct extension("local"){
function m1()
function m2()
}
struct extension("cppsp.test.mytype"){
function n1(){
print("test1\n")
}
}
struct extension("local"){
function m3(){
print("test2\n")
}
}
local o
o.m3()
cppsp.test.mytype oo
oo.n1()
```
### Warning ⚠️
* Cannot accept any space/blank before keyword before v1.2!
* No multi-line before v1.3!
* `@command()` will never be multi-line but you can use following as an alternative
```
@command("-f1 -f2 ..... -f5")
@command("-f6 -f7 ....-f10")
```
with `#overwrite` command
```
@command("g++ -Os -m64 -nostdlib -shared ")
@command(" -o dll.dll dll.cpp")
```
## Example
```cpp
print("hello world")
```
* another exmaple:
```cpp
@command("-mtune=native -fomit-frame-pointer -static-libgcc -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--as-needed -s -Wl,--strip-all -Os -m64")
import iostream,vector
@function<>
print("12\n"," ",1," ",2.1,true,false," ")
print( "abc")
print(1,"\n") //abv
//print(1.1)
@inject(int x=1;int y=2;int z=3; auto is_bool = [](const std::string& s){ return s == "true" || s == "false";};)
input(x,y,z)
@function< cars = {"Volvo", "BMW", "Ford", "Mazda"};};>>
print(x+y+z)
```
* simple dll
```cpp
#overwrite
@command("g++ -Os -m64 -nostdlib -shared -o dll.dll dll.cpp")
@function<>
```
* [swap](example/swap.cppsp)