Skip to content

Commit 93873f9

Browse files
committed
fixed errors and added CHEATSHEET.md
1 parent cb946f3 commit 93873f9

5 files changed

Lines changed: 174 additions & 4 deletions

File tree

CHEATSHEET.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# ProXPL Cheatsheet
2+
3+
A quick reference guide for the ProX Programming Language (ProXPL).
4+
5+
## 1. Basics
6+
7+
### Comments
8+
```javascript
9+
// Single-line comment
10+
/* Multi-line
11+
comment */
12+
```
13+
14+
### Variables
15+
ProXPL uses `let` for mutable variables and `const` for constants.
16+
```javascript
17+
let x = 10;
18+
x = 20;
19+
20+
const PI = 3.14;
21+
// PI = 3.14159; // Error: Assignment to constant
22+
```
23+
24+
### Data Types
25+
- **Primitive**: `int`, `float`, `bool`, `string`, `null`.
26+
- **Composite**: `List`, `Dictionary` (Map).
27+
28+
```javascript
29+
let isDone = true;
30+
let count = 42;
31+
let price = 19.99;
32+
let name = "ProX";
33+
let empty = null;
34+
let list = [1, 2, 3];
35+
// let map = {"key": "value"}; // (Syntax supported in some contexts)
36+
```
37+
38+
## 2. Control Flow
39+
40+
### If-Else
41+
```javascript
42+
if (score > 90) {
43+
print("Excellent");
44+
} else if (score > 50) {
45+
print("Pass");
46+
} else {
47+
print("Fail");
48+
}
49+
```
50+
51+
### Loops
52+
```javascript
53+
// While Loop
54+
let i = 0;
55+
while (i < 5) {
56+
print(i);
57+
i = i + 1;
58+
}
59+
60+
// For Loop
61+
for (let j = 0; j < 5; j = j + 1) {
62+
print(j);
63+
}
64+
```
65+
66+
## 3. Functions
67+
68+
### Declaration
69+
```javascript
70+
func add(a, b) {
71+
return a + b;
72+
}
73+
74+
let sum = add(5, 3);
75+
```
76+
77+
### Arrow Functions (Short syntax)
78+
```javascript
79+
func square(x) => x * x;
80+
```
81+
82+
## 4. Object-Oriented Programming
83+
84+
### Classes
85+
```javascript
86+
class Animal {
87+
func speak() {
88+
print("Generic sound");
89+
}
90+
}
91+
92+
class Dog extends Animal {
93+
func speak() {
94+
print("Woof!");
95+
}
96+
}
97+
```
98+
99+
### Instantiation
100+
```javascript
101+
let d = new Dog();
102+
d.speak(); // Output: Woof!
103+
```
104+
105+
### Usage of `this`
106+
```javascript
107+
class Person {
108+
func setAge(a) {
109+
this.age = a;
110+
}
111+
}
112+
```
113+
114+
## 5. Standard Library
115+
116+
### IO (`std.io`)
117+
```javascript
118+
print("Hello World"); // Native print
119+
// or
120+
use std.io;
121+
IO.println("Hello World");
122+
let name = IO.readLine();
123+
```
124+
125+
### Math (`std.math`)
126+
```javascript
127+
use std.math;
128+
129+
let maxVal = Math.max(10, 20);
130+
let rand = Math.random();
131+
let root = Math.sqrt(16);
132+
```
133+
134+
### Native Functions
135+
Built-in global functions available without imports:
136+
- `print(msg)`
137+
- `input(prompt)`
138+
- `to_string(value)`
139+
- `clock()` - Returns current time
140+
- `random(min, max)` (If available in specific implementations)
141+
142+
## 6. Operators
143+
- **Arithmetic**: `+`, `-`, `*`, `/`, `%`
144+
- **Comparison**: `==`, `!=`, `<`, `>`, `<=`, `>=`
145+
- **Logical**: `&&`, `||`, `!`
146+
147+
## 7. Example Program
148+
```javascript
149+
// fib.prox
150+
func fib(n) {
151+
if (n <= 1) return n;
152+
return fib(n - 1) + fib(n - 2);
153+
}
154+
155+
func main() {
156+
print(fib(10));
157+
}
158+
159+
main();
160+
```

benchmarks/macro/json_bench.prox

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func main() {
112112
let parser = JsonParser(json_str);
113113
let res = parser.parse();
114114

115-
let elapsed = time() - start;
115+
let elapsed = clock() - start;
116116

117117
// Check result
118118
let users = res["users"];

src/compiler/type_checker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static TypeInfo checkBinary(TypeChecker* checker, Expr* expr) {
127127
// Let's be safe: if unknown, we can't guarantee safety, but usually in static analysis we warn.
128128
// Here we will allow it to proceed as Unknown to avoid cascading errors.
129129
// DEBUG:
130-
printf("Binary '%s' L:%d R:%d at line %d\n", op, l.kind, r.kind, expr->line);
130+
// printf("Binary '%s' L:%d R:%d at line %d\n", op, l.kind, r.kind, expr->line);
131131

132132
if (l.kind == TYPE_UNKNOWN || r.kind == TYPE_UNKNOWN) {
133133
return createType(TYPE_UNKNOWN);

src/runtime/vm.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,18 +467,30 @@ static InterpretResult run(VM* vm) {
467467
DISPATCH();
468468
}
469469
DO_OP_SUBTRACT: {
470+
if (!IS_NUMBER(peek(vm, 0)) || !IS_NUMBER(peek(vm, 1))) {
471+
runtimeError(vm, "Operands must be numbers.");
472+
return INTERPRET_RUNTIME_ERROR;
473+
}
470474
double b = AS_NUMBER(pop(vm));
471475
double a = AS_NUMBER(pop(vm));
472476
push(vm, NUMBER_VAL(a - b));
473477
DISPATCH();
474478
}
475479
DO_OP_MULTIPLY: {
480+
if (!IS_NUMBER(peek(vm, 0)) || !IS_NUMBER(peek(vm, 1))) {
481+
runtimeError(vm, "Operands must be numbers.");
482+
return INTERPRET_RUNTIME_ERROR;
483+
}
476484
double b = AS_NUMBER(pop(vm));
477485
double a = AS_NUMBER(pop(vm));
478486
push(vm, NUMBER_VAL(a * b));
479487
DISPATCH();
480488
}
481489
DO_OP_DIVIDE: {
490+
if (!IS_NUMBER(peek(vm, 0)) || !IS_NUMBER(peek(vm, 1))) {
491+
runtimeError(vm, "Operands must be numbers.");
492+
return INTERPRET_RUNTIME_ERROR;
493+
}
482494
double b = AS_NUMBER(pop(vm));
483495
double a = AS_NUMBER(pop(vm));
484496
push(vm, NUMBER_VAL(a / b));

src/stdlib/stdlib_core.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ extern Value native_clock(int argCount, Value* args);
3333
static Value native_len(int argCount, Value* args) {
3434
if (argCount < 1) return NUMBER_VAL(0);
3535
if (IS_STRING(args[0])) return NUMBER_VAL((double)AS_STRING(args[0])->length);
36-
printf("native_len: Type=%d, IS_LIST=%d, IS_STRING=%d\n", AS_OBJ(args[0])->type, IS_LIST(args[0]), IS_STRING(args[0]));
3736
if (IS_STRING(args[0])) return NUMBER_VAL((double)AS_STRING(args[0])->length);
3837
if (IS_LIST(args[0])) {
39-
printf("It is a list! Count: %d\n", AS_LIST(args[0])->count);
4038
return NUMBER_VAL((double)AS_LIST(args[0])->count);
4139
}
4240
if (IS_DICTIONARY(args[0])) return NUMBER_VAL((double)AS_DICTIONARY(args[0])->items.count);

0 commit comments

Comments
 (0)