Skip to content

Commit

Permalink
Merge pull request #8 from EpitechPromo2026/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rclovis authored Jan 16, 2024
2 parents 67b89f0 + 822d8ac commit 9bbe39d
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 15 deletions.
10 changes: 10 additions & 0 deletions Ftests/Input5.fk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "funkylib.fk"

var str: u8[] = "Very Strong string really difficult to break and very long as you can see";

putnbr(strlen(str));
write 10;
putstr(str);
write 10;
putstrrev(str);
write 10;
3 changes: 3 additions & 0 deletions Ftests/Output5
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
73
Very Strong string really difficult to break and very long as you can see
ees nac uoy sa gnol yrev dna kaerb ot tluciffid yllaer gnirts gnortS yreV
105 changes: 105 additions & 0 deletions Ftests/brainFunk.fk
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "funkylib.fk"

#define OP_END 0
#define OP_INC_DP 1
#define OP_DEC_DP 2
#define OP_INC_VAL 3
#define OP_DEC_VAL 4
#define OP_OUT 5
#define OP_INS 6
#define OP_JMP_FWD 7
#define OP_JMP_BCK 8

#define SUCCESS 0
#define FAILURE 1

#define PROGRAM_SIZE 4096
#define STACK_SIZE 512
#define DATA_SIZE 4096

allocate PROGRAM: u16 = PROGRAM_SIZE * 2;
allocate STACK: u16 = STACK_SIZE;
var SP: u32 = 0;



var data1: u64 = _argv[1];
var pc: u16 = 0;
var jmp_pc: u16 = 0;
var i: i32 = 0;
while (data1[i] != 0) {
if (data1[i] == 62) {
PROGRAM[pc * 2] = OP_INC_DP;
}
if (data1[i] == 60) {
PROGRAM[pc * 2] = OP_DEC_DP;
}
if (data1[i] == 43) {
PROGRAM[pc * 2] = OP_INC_VAL;
}
if (data1[i] == 45) {
PROGRAM[pc * 2] = OP_DEC_VAL;
}
if (data1[i] == 46) {
PROGRAM[pc * 2] = OP_OUT;
}
if (data1[i] == 44) {
PROGRAM[pc * 2] = OP_INS;
}
if (data1[i] == 91) {
PROGRAM[pc * 2] = OP_JMP_FWD;
STACK[SP] = pc;
SP = SP + 1;
}
if (data1[i] == 93) {
SP = SP - 1;
jmp_pc = STACK[SP];
PROGRAM[pc * 2] = OP_JMP_BCK;
PROGRAM[pc * 2 + 1] = jmp_pc;
PROGRAM[jmp_pc * 2 + 1] = pc;
}
i = i + 1;
pc = pc + 1;
}

PROGRAM[pc * 2] = OP_END;

pc = 0;
allocate data2: u64 = DATA_SIZE;
var ptr: u32 = DATA_SIZE;
while (ptr != 0) {
data2[ptr] = 0;
ptr = ptr - 1;
}
while (PROGRAM[pc * 2] != OP_END) {
if (PROGRAM[pc * 2] == OP_INC_DP) {
ptr = ptr + 1;
}
if (PROGRAM[pc * 2] == OP_DEC_DP) {
ptr = ptr - 1;
}
if (PROGRAM[pc * 2] == OP_INC_VAL) {
data2[ptr] = data2[ptr] + 1;
}
if (PROGRAM[pc * 2] == OP_DEC_VAL) {
data2[ptr] = data2[ptr] - 1;
}
if (PROGRAM[pc * 2] == OP_OUT) {
write data2[ptr];
}
if (PROGRAM[pc * 2] == OP_INS) {
data2[ptr] = 0;
}
if (PROGRAM[pc * 2] == OP_JMP_FWD) {
if (data2[ptr] == 0) {
pc = PROGRAM[pc * 2 + 1];
}
}
if (PROGRAM[pc * 2] == OP_JMP_BCK) {
if (data2[ptr] != 0) {
pc = PROGRAM[pc * 2 + 1];
}
}
pc = pc + 1;
}
return 0;
69 changes: 69 additions & 0 deletions Ftests/funkylib.fk
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
funk strlen(str: u64): i32 {
var i: i32 = 0;
while (str[i] != 0) {
i = i + 1;
}
return i;
}

funk putstr(str: u64): i32 {
var size: i32 = strlen(str);
var i: i32 = 0;
while (i < size) {
write str[i];
i = i + 1;
}
return 0;
}

funk putnbr(nb: i32): i32 {
var pow: i32 = 1;
var save: i32 = nb;

if (nb == 0) {
write 48;
return 0;
}

if (nb < 0) {
write 45;
nb = nb * -1;
}

while (save > 0) {
pow = pow * 10;
save = save / 10;
}
pow = pow / 10;

while (pow > 0) {
write ((nb / pow) + 48);
nb = nb % pow;
pow = pow / 10;
}
return 0;
}

funk putstrrev(str: u64): i32 {
var size: i32 = strlen(str);
var i: i32 = size - 1;
while (i >= 0) {
write str[i];
i = i - 1;
}
return 0;
}

funk strcmp(str1: u64, str2: u64): i32 {
var i: i32 = 0;
while (str1[i] != 0 && str2[i] != 0) {
if (str1[i] != str2[i]) {
return 1;
}
i = i + 1;
}
if (str1[i] != str2[i]) {
return 1;
}
return 0;
}
4 changes: 3 additions & 1 deletion brainFunk.fk
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ while (ptr != 0) {
data2[ptr] = 0;
ptr = ptr - 1;
}
var tmp: u8 = 0;
while (PROGRAM[pc * 2] != OP_END) {
if (PROGRAM[pc * 2] == OP_INC_DP) {
ptr = ptr + 1;
Expand All @@ -88,7 +89,8 @@ while (PROGRAM[pc * 2] != OP_END) {
write data2[ptr];
}
if (PROGRAM[pc * 2] == OP_INS) {
data2[ptr] = 0;
read tmp;
data2[ptr] = tmp;
}
if (PROGRAM[pc * 2] == OP_JMP_FWD) {
if (data2[ptr] == 0) {
Expand Down
14 changes: 6 additions & 8 deletions documentation/docs/Technical Documentation/Funk Language/Bnf.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# Glados

#Grammar Rules

1.Program Structure

```bnf
```bnf
<program> ::= { <declaration> }*
```

2.Declarations:
```bnf
```bnf
<declaration> ::= <function-definition> | <pointer-definition> | <import-statement>
```
```

3.Function Definition:
```bnf
Expand All @@ -24,7 +22,7 @@
```

5.Parameter:
```bnf
```bnf
<parameter> ::= <variable-name> ":" <data-type>
```

Expand All @@ -39,12 +37,12 @@
```

8.Expressions:
```bnf
```bnf
<expression> ::= <variable-name> | <literal> | <binary-operation> | <unary-operation> | "(" <expression> ")"
```

9.Binary Operation:
```bnf
```bnf
<binary-operation> ::= <expression> <binary-operator> <expression>
```

Expand Down
64 changes: 64 additions & 0 deletions documentation/docs/User Documentation/Funk_Introduction_cn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
## The Funk编程语言

欢迎来到 *The Funk Programming Language*,这是有关Funk的介绍。Funk是一种受Rust和C传统启发的高级和低级控制语言。Funk注重现代语言的优雅语法,同时提供对代码的控制。

## Funk适用于谁?

Funk适用于那些程序员,用一个词来形容就是“时髦的”。
在Funk基金会,我们希望有一种语言,能让来自各行各业和背景的人都能拥抱他们的真我。成为Funk程序员不仅仅是简历上的一项技能,更是一种精神

## 法律事项

Funk是一种开源语言,欢迎新的贡献者。写Funk代码的唯一要求是要时髦。

## 入门指南

让我们开始你的Funk之旅吧!有很多东西要学,但每个旅程都要从某个地方开始。我们将讨论在Funk中编写*Hello World!*程序以及如何使用语法。

## Hello World

Hello World!这是经典的、每次学习新东西时都会写的第一个程序。在Funk中我们也会做同样的事情!

首先,创建一个新的项目文件夹和一个名为 `hello.fk` 的文件。Funk使用 `.fk` 扩展名来表示其源文件。

通过使用 `var` 关键字创建一个变量。给它一个名字,然后使用 `:` 符号指定类型。Funk有多种原始类型,我们将在后面的章节中介绍,现在我们想要存储一个字符串。我们将使用 `u8[]` 来存储我们的字符串。然后使用 = 符号添加一个值。在这里指定我们的消息。用必需的 `;` 分号结束你的指令,这样你就创建了你的第一个变量!

```ts
var hello: u8[] = "Hello World!";
```

现在,为了打印它,我们将使用Funk的标准库:`funkystd`

在你的文件顶部添加以下内容 `"#include "funkystd"`。这将为你的程序提供比以往更多的功能实用性!现在我们可以使用库中的 `putstr` 函数来打印我们的消息。

```ts
#include "funkystd"

var hello: u8[] = "Hello World!";

putstrln(hello);
```

恭喜!你刚刚在Funk中编写了你的第一个程序!但等一下,我们还没有运行它!不要担心,我们将看到如何在下一章中运行Funk程序。

## 运行Funk程序

你刚刚完成了你的第一个Funk *Hello World!*,但你仍然不知道如何运行它?在本章中,我们将介绍Funk的执行方式。

Funk是一种**编译**语言,这意味着它需要被放入另一个程序中才能执行。但别担心,我们已经为你准备好了,你不必自己编写。使用命令行界面,输入以下命令:

```console
$ funkc hello.fk
Created: out.bin
```

你得到了一个新文件:`out.bin`。这个文件代表我们的程序的`字节码`。与其他编译语言一样,Funk首先被编译成包含程序所需指令集的字节码。

这个文件不是一个可执行二进制文件,而是字节码。现在我们可以使用`FVM`(Funk虚拟机)来执行字节码指令。想象一下Java的执行方式。你将项目编译成一个包含字节码的 `.jar` 文件,然后将其传递给`JVM`(Java虚拟机)。这不是剽窃,这是灵感来源。

```console
$ fvm out.bin
Hello World!
```

现在它正在运行!你的第一个程序,现在在终端上显示输出。
1 change: 1 addition & 0 deletions funkylib.fk
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ funk strcmp(str1: u64, str2: u64): i32 {
}
return 0;
}

4 changes: 3 additions & 1 deletion program.fk
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
var test: i32 = 125 + 5;
var str1: u8 = 0;
read str1;
write str1;
3 changes: 3 additions & 0 deletions src/Ast/Ast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ data Ast
| Call String [Ast]
| Return Ast
| Write Ast
| Read String
| Malloc Ast
| Free Ast
| Exit Ast
Expand Down Expand Up @@ -337,6 +338,8 @@ getKeywordArg (A Lexer.Exit : xs) = do
let (value, xs') = takeUntil (== A End) xs
(expr, _) <- getAst value
pure (Ast.Ast.Exit expr, xs')
getKeywordArg (A (Identifier "read") : A (Identifier name) : A End : xs) = do
pure (Ast.Ast.Read name, xs)
getKeywordArg _ = Nothing

getNumber :: [Expr] -> Maybe Ast
Expand Down
19 changes: 19 additions & 0 deletions src/BuildBytecode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,21 @@ getAllocate (Ast.Ast.Allocate name t ast) = MemoryState $ do
put stock2 {bytecode = bytecode stock2 |> Ustore 2 (correspondingInt 2 (toInteger (memoryGetVarIndex name (index (memVar stock2) 0))))}
getAllocate _ = return ()

getRead :: Ast -> MemoryState ()
getRead (Read name) = MemoryState $ do
stock <- get
put stock {bytecode = bytecode stock |> Bytecode.GetChar}
stock2 <- get
put stock2 {bytecode = bytecode stock2 |> Ustore 2 (correspondingInt 2 (toInteger (memoryGetVarIndex name (index (memVar stock2) 0))))}
getRead _ = return ()

getExit :: Ast -> MemoryState ()
getExit (Ast.Ast.Exit num) = MemoryState $ do
runMemoryState (getAll num)
stock <- get
put stock {bytecode = bytecode stock |> Bytecode.Exit}
getExit _ = return ()

getAll :: Ast -> MemoryState ()
getAll (Seq asts) = MemoryState $ do
runMemoryState (getSeq (Seq asts))
Expand Down Expand Up @@ -521,6 +536,10 @@ getAll (Ast.Ast.Write ast) = MemoryState $ do
runMemoryState (getWrite (Ast.Ast.Write ast))
getAll (Ast.Ast.Allocate name t ast) = MemoryState $ do
runMemoryState (getAllocate (Ast.Ast.Allocate name t ast))
getAll (Read name) = MemoryState $ do
runMemoryState (getRead (Read name))
getAll (Ast.Ast.Exit num) = MemoryState $ do
runMemoryState (getExit num)
getAll _ = return ()

bcSecToList :: S.Seq Bytecode -> [Bytecode]
Expand Down
Loading

0 comments on commit 9bbe39d

Please sign in to comment.