Skip to content

Commit 6e1316e

Browse files
Merge pull request #18 from FernandoTheDev/fernando
Update to version 0.0.2
2 parents 9640cc9 + 1e803bb commit 6e1316e

32 files changed

Lines changed: 1350 additions & 203 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ tests/trash
22
a.out
33
bin
44
farpy
5+
*.ll
6+
repl.fp

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Farpy is a statically-typed, compiled programming language designed for performance, safety, and simplicity. Written entirely in TypeScript, Farpy generates a lean and highly optimized binary without relying on external dependencies. Its standard library is implemented in C to maximize execution speed and maintain a compact footprint.
88

9-
**Complete documentation**: [Doc](doc/) | **Version:** `0.0.1`
9+
**Complete documentation**: [Doc](doc/) | **Version:** `0.0.2`
1010

1111
## Key Features
1212

@@ -53,19 +53,19 @@ Ensure that all tools are available in your system `PATH`.
5353
```
5454
3. **Build the compiler**:
5555
```bash
56-
deno task compile
56+
./build.sh install
5757
```
5858
4. **Compile a Farpy program**:
5959
```bash
60-
./farpy examples/hello.fp --opt --o hello
60+
farpy examples/hello.fp --opt --o hello
6161
```
6262
5. **Run the generated executable**:
6363
```bash
6464
./hello
6565
```
6666
6. **See how to use compiler flags**
6767
```bash
68-
./farpy --h
68+
farpy --h
6969
```
7070

7171
## Contributing

build.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
BIN_NAME="farpy"
5+
INSTALL_PATH="/usr/local/bin/$BIN_NAME"
6+
7+
function build() {
8+
echo "🔧 Building $BIN_NAME..."
9+
deno task compile
10+
echo "✅ Build finished."
11+
}
12+
13+
function install_bin() {
14+
build
15+
echo "📦 Installing $BIN_NAME to $INSTALL_PATH..."
16+
sudo install -m 0755 "$BIN_NAME" "$INSTALL_PATH"
17+
echo "✅ Installed successfully!"
18+
}
19+
20+
function remove_bin() {
21+
echo "🗑️ Removing $INSTALL_PATH..."
22+
if [[ -f "$INSTALL_PATH" ]]; then
23+
sudo rm -f "$INSTALL_PATH"
24+
echo "✅ Removed successfully!"
25+
else
26+
echo "⚠️ $INSTALL_PATH not found. Nothing to remove."
27+
fi
28+
}
29+
30+
case "${1:-}" in
31+
install)
32+
install_bin
33+
;;
34+
remove)
35+
remove_bin
36+
;;
37+
*)
38+
echo "Usage: $0 {install|remove}"
39+
exit 1
40+
;;
41+
esac

cli/repl.ts

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/**
2+
* Farpy - A programming language
3+
*
4+
* Copyright (c) 2025 Fernando (FernandoTheDev)
5+
*
6+
* This software is licensed under the MIT License.
7+
* See the LICENSE file in the project root for full license information.
8+
*/
9+
import { VERSION } from "../config.ts";
10+
import { FarpyCompilerMain } from "../farpy.ts";
11+
12+
export async function repl(): Promise<void> {
13+
let code: string[] = [];
14+
let compiled = false;
15+
const history: string[] = [];
16+
17+
const file = "repl.fp";
18+
const binary = file.replace(".fp", "");
19+
20+
Deno.writeFileSync(file, new TextEncoder().encode(""));
21+
22+
const helpMessage = `Farpy REPL Commands:
23+
. - Run the compiled binary
24+
; - Compile the current code
25+
clb - Clear code buffer
26+
cll - Clear last line of code
27+
swb - Show code buffer
28+
hist - Show command history
29+
help - Show this help message
30+
q/quit - Exit the REPL
31+
`;
32+
33+
console.log(`Farpy ${VERSION} - REPL started. Type 'help' for commands.`);
34+
35+
while (true) {
36+
const temp_code = prompt("farpy> ");
37+
38+
if (!temp_code || temp_code.trim() === "") {
39+
continue;
40+
}
41+
42+
if (!["hist", "swb"].includes(temp_code)) {
43+
history.push(temp_code);
44+
}
45+
46+
if (temp_code === "help") {
47+
console.log(helpMessage);
48+
continue;
49+
}
50+
51+
if (temp_code === "hist") {
52+
if (history.length === 0) {
53+
console.log("No command history.");
54+
} else {
55+
console.log("Command history:");
56+
history.forEach((cmd, i) => console.log(`${i + 1}: ${cmd}`));
57+
}
58+
continue;
59+
}
60+
61+
if (temp_code === ".") {
62+
if (!compiled) {
63+
console.log("There is no compiled binary to run.");
64+
continue;
65+
}
66+
67+
try {
68+
const command = new Deno.Command(`./${binary}`);
69+
const { code: exitCode, stdout, stderr } = await command.output();
70+
71+
if (exitCode !== 0) {
72+
console.log(`Error: ${new TextDecoder().decode(stderr)}`);
73+
continue;
74+
}
75+
76+
console.log(new TextDecoder().decode(stdout));
77+
} catch (error: any) {
78+
console.log(`Execution error: ${error.message}`);
79+
}
80+
continue;
81+
}
82+
83+
if (temp_code === ";") {
84+
if (code.length === 0) {
85+
console.log("No code to compile.");
86+
continue;
87+
}
88+
89+
try {
90+
Deno.writeTextFileSync(file, code.join("\n"));
91+
92+
console.log("Starting compilation...");
93+
const compiler = new FarpyCompilerMain([file, "--o", binary]);
94+
await compiler.run();
95+
96+
compiled = true;
97+
console.log("Code compiled successfully. Run with '.'");
98+
} catch (error: any) {
99+
console.log(`Compilation error: ${error.message}`);
100+
}
101+
continue;
102+
}
103+
104+
if (temp_code === "clb") {
105+
code = [];
106+
console.log("Code buffer cleared.");
107+
continue;
108+
}
109+
110+
if (temp_code === "cll") {
111+
if (code.length > 0) {
112+
const removed = code.pop();
113+
console.log(`Line removed: ${removed!.trim()}`);
114+
} else {
115+
console.log("Buffer is already empty.");
116+
}
117+
continue;
118+
}
119+
120+
if (temp_code === "swb") {
121+
if (code.length === 0) {
122+
console.log("Code buffer is empty.");
123+
} else {
124+
console.log("Current code buffer:");
125+
console.log("----------------");
126+
console.log(code.join(""));
127+
console.log("----------------");
128+
}
129+
continue;
130+
}
131+
132+
if (temp_code === "q" || temp_code === "quit" || temp_code === "sair") {
133+
console.log("Exiting Farpy REPL. Bye bye!");
134+
break;
135+
}
136+
137+
code.push(temp_code.endsWith("\n") ? temp_code : `${temp_code}\n`);
138+
console.log("Line added to buffer.");
139+
}
140+
141+
// Cleanup files when exiting
142+
try {
143+
if (Deno.statSync(file).isFile) {
144+
Deno.removeSync(file);
145+
}
146+
147+
if (compiled && Deno.statSync(binary).isFile) {
148+
Deno.removeSync(binary);
149+
}
150+
} catch (_error) {
151+
// File might not exist, that's okay
152+
}
153+
154+
console.log("Files cleaned up.");
155+
}

config.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const ARG_CONFIG = {
2+
alias: {
3+
h: "help",
4+
v: "version",
5+
astj: "ast-json",
6+
astjs: "ast-json-save",
7+
o: "output",
8+
eir: "emit-llvm-ir",
9+
opt: "optimize",
10+
dc: "dead-code",
11+
cli: "repl",
12+
},
13+
boolean: [
14+
"help",
15+
"version",
16+
"ast-json",
17+
"debug",
18+
"emit-llvm-ir",
19+
"targeth",
20+
"optmize",
21+
"g",
22+
"dead-code",
23+
"repl",
24+
],
25+
string: ["ast-json-save", "output", "target"],
26+
default: { "ast-json-save": "ast.json", "output": "a.out" },
27+
};
28+
29+
const VERSION = "0.0.2";
30+
31+
const TARGET_HELP_MESSAGE = `Farpy Compiler - Target Architecture Help
32+
33+
Supports the following target architectures:
34+
x86_64-linux-gnu - 64-bit x86, Linux
35+
i386-linux-gnu - 32-bit x86, Linux
36+
aarch64-linux-gnu - ARM 64-bit, Linux
37+
arm-linux-gnueabi - ARM 32-bit (EABI), Linux
38+
armv7-linux-gnueabihf - ARM 32-bit (hard-float ABI), Linux`;
39+
40+
const HELP_MESSAGE = `Farpy Compiler ${VERSION}
41+
42+
USAGE:
43+
farpy [OPTIONS] <FILE>
44+
45+
OPTIONS:
46+
-h, --help Show this help message
47+
-v, --version Display version information
48+
--ast-json Output AST as JSON and exit
49+
--ast-json-save=<file> Save AST JSON to specified file (default: ast.json)
50+
-o, --output=<file> Specify output file name (default: a.out)
51+
--opt, --optimize Enable optimization in AST
52+
--debug Enable debug mode
53+
--emit-llvm-ir Output LLVM IR and exit
54+
--target=<target> Specify target architecture (default: your architecture)
55+
--targeth Show target architecture help
56+
--repl, --cli Open the compiled repl mode`;
57+
58+
export { ARG_CONFIG, HELP_MESSAGE, TARGET_HELP_MESSAGE, VERSION };

doc/EN.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Control Flow](#control-flow)
1515
- [If / Elif / Else](#if--elif--else)
1616
- [For](#for)
17+
- [While](#while)
1718
- [Standard Libraries](#standard-libraries)
1819
- [`io`](#io)
1920
- [Importing External Code](#importing-external-code)
@@ -51,9 +52,8 @@ Farpy is a general-purpose language combining simplicity, safety, and high perfo
5152
2. Compile the compiler:
5253

5354
```bash
54-
deno task compile
55+
./build.sh install
5556
```
56-
3. Add `farpy` to your PATH (e.g., move it to `/usr/local/bin`).
5757

5858
---
5959

@@ -168,6 +168,15 @@ for 0..=100 step 2 as i {
168168
}
169169
```
170170

171+
### While
172+
173+
```farpy
174+
while i < 10 {
175+
i = i + 1 // i++
176+
// do something
177+
}
178+
```
179+
171180
---
172181

173182
## Standard Libraries
@@ -270,7 +279,7 @@ farpy file.fp --opt
270279

271280
## Version
272281

273-
* **Current Version:** 0.0.1
282+
* **Current Version:** 0.0.2
274283

275284
---
276285

doc/PT-BR.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Controle de Fluxo](#controle-de-fluxo)
1515
- [If / Elif / Else](#if--elif--else)
1616
- [For](#for)
17+
- [While](#while)
1718
- [Bibliotecas Padrão](#bibliotecas-padrão)
1819
- [`io`](#io)
1920
- [Importando Código Externo](#importando-código-externo)
@@ -51,9 +52,8 @@ Farpy é uma linguagem de propósito geral que combina simplicidade, segurança
5152
2. Compile o compilador:
5253

5354
```bash
54-
deno task compile
55+
./build.sh install
5556
```
56-
3. Adicione `farpy` ao seu PATH (ex.: movendo para `/usr/local/bin`).
5757

5858
---
5959

@@ -168,6 +168,15 @@ for 0..=100 step 2 as i {
168168
}
169169
```
170170

171+
### While
172+
173+
```farpy
174+
while i < 10 {
175+
i = i + 1 // i++
176+
// faça algo
177+
}
178+
```
179+
171180
---
172181

173182
## Bibliotecas Padrão
@@ -270,7 +279,7 @@ farpy file.fp --opt
270279

271280
## Versão
272281

273-
* **Versão Atual:** 0.0.1
282+
* **Versão Atual:** 0.0.2
274283

275284
---
276285

0 commit comments

Comments
 (0)