oclang 0.3.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BUG_REGISTRY.md +14 -0
- package/CHANGELOG.md +46 -10
- package/LICENSE +21 -0
- package/README.md +15 -15
- package/package.json +30 -19
- package/src/cli/index.ts +0 -0
- package/src/cli/ocat/commands/index.ts +2 -0
- package/src/cli/ocat/commands/repl.ts +49 -0
- package/src/cli/ocat/commands/run.ts +32 -0
- package/src/cli/ocat/index.ts +26 -0
- package/src/cli/ocm/commands/index.ts +2 -0
- package/src/cli/ocm/commands/init.ts +49 -0
- package/src/cli/ocm/commands/run.ts +20 -0
- package/src/cli/ocm/index.ts +22 -0
- package/src/cli/utils/chalkText.ts +8 -0
- package/src/core/ast/astBuilder.ts +11 -17
- package/src/core/ast/types/base/index.ts +2 -2
- package/src/core/ast/types/statements/callStatement.ts +1 -0
- package/src/core/ast/types/statements/functionStatement.ts +2 -10
- package/src/core/ast/types/statements/index.ts +13 -1
- package/src/core/index.ts +15 -6
- package/src/core/lexer/tokens/delimiters.ts +3 -1
- package/src/core/runner/runner.ts +9 -4
- package/src/core/runner/utils/string.ts +3 -3
- package/src/core/services/log.service.ts +77 -0
- package/src/project/index.ts +37 -0
- package/src/project/models/project.ts +11 -0
- package/src/project/utils/project.ts +12 -0
- package/src/shared/context/globalContext.ts +25 -0
- package/src/shared/io/json.ts +9 -0
- package/src/shared/manager/baseManager.ts +19 -4
- package/src/shared/manager/errors/coreErrors.ts +3 -4
- package/src/shared/manager/errors/io/extension.ts +8 -0
- package/src/shared/manager/errors/io/file.ts +8 -0
- package/src/shared/manager/errors/semantic/undefined/declared.ts +4 -4
- package/src/shared/manager/errors/semantic/undefined/undeclared.ts +3 -3
- package/src/shared/manager/errors/syntax/syntaxErrors.ts +1 -1
- package/src/shared/manager/warn/coreWarnings.ts +2 -1
- package/src/shared/models/func.ts +9 -0
- package/src/shared/utils/objects.ts +7 -0
- package/src/shared/utils/strformat.ts +15 -0
- package/tsconfig.json +28 -27
- package/src/index.ts +0 -8
- package/src/shared/manager/errors/api/index.ts +0 -2
- package/src/shared/manager/errors/api/throw.ts +0 -5
- package/src/shared/manager/errors/api/types.ts +0 -5
package/BUG_REGISTRY.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Bug Registry
|
|
2
|
+
|
|
3
|
+
## Format
|
|
4
|
+
|
|
5
|
+
BUG-\<BUG-NUMBER>\<BUG-PART>-\<VERSION>-\<VERSION-FORMAT>
|
|
6
|
+
|
|
7
|
+
## 1.X.X bugs
|
|
8
|
+
|
|
9
|
+
- BUG-002A-100-MmP: CLI always uses force mode
|
|
10
|
+
- BUG-002B-101-MmP: The errors in the CLI shows 'undefined'
|
|
11
|
+
|
|
12
|
+
## 0.3.0 bugs
|
|
13
|
+
|
|
14
|
+
- BUG-001A-030-MmP: Functions don't work
|
package/CHANGELOG.md
CHANGED
|
@@ -4,24 +4,60 @@
|
|
|
4
4
|
|
|
5
5
|
MAJOR.MINOR.PATCH
|
|
6
6
|
|
|
7
|
-
MAJOR version when you make incompatible and breaking changes
|
|
7
|
+
MAJOR version when you make incompatible and breaking changes.
|
|
8
|
+
|
|
8
9
|
MINOR version when you add functionality in a backwards-compatible manner, and
|
|
10
|
+
|
|
9
11
|
PATCH version when you make backwards-compatible bug fixes.
|
|
10
12
|
|
|
11
|
-
##
|
|
13
|
+
## 1.X.X - Beta and CLI
|
|
12
14
|
|
|
13
|
-
-
|
|
14
|
-
- Basic output `print("Hello World")`
|
|
15
|
-
- Basic variable declaration `number x = 10`
|
|
16
|
-
- Basic printing of variables `print(x)`
|
|
15
|
+
### 1.2.0 - OCM
|
|
17
16
|
|
|
18
|
-
|
|
17
|
+
- OCM mode added
|
|
19
18
|
|
|
20
|
-
|
|
19
|
+
#### 1.2.1 - Uploading error
|
|
20
|
+
|
|
21
|
+
- Uploading error in 1.2.0
|
|
22
|
+
- Updated the CLI ```ocm init``` command
|
|
23
|
+
|
|
24
|
+
### 1.1.0 - REPL
|
|
25
|
+
|
|
26
|
+
- REPL mode added
|
|
27
|
+
|
|
28
|
+
#### 1.1.1 - BUG-002B-101-MmP
|
|
29
|
+
|
|
30
|
+
- BUG-002B-100-MmP: The errors in the CLI shows 'undefined'
|
|
31
|
+
- FileDoesntExistError and ExtensionError added
|
|
32
|
+
|
|
33
|
+
### 1.0.0 - Alpha to beta migration
|
|
34
|
+
|
|
35
|
+
- Basic CLI
|
|
21
36
|
|
|
22
|
-
|
|
37
|
+
#### 1.0.1 - BUG-002A-100-MmP
|
|
38
|
+
|
|
39
|
+
- BUG-002A-100-MmP: CLI always uses force mode
|
|
40
|
+
|
|
41
|
+
## 0.X.X - Alpha
|
|
42
|
+
|
|
43
|
+
### 0.3.0 - Generics
|
|
23
44
|
|
|
24
45
|
- Error and warning handling system
|
|
25
46
|
- Functions
|
|
26
47
|
- Constants and set. Constants can't be modified
|
|
27
|
-
|
|
48
|
+
- AST manager added
|
|
49
|
+
|
|
50
|
+
#### 0.3.1 - BUG-001A-030-MmP
|
|
51
|
+
|
|
52
|
+
- BUG-001A-030-MmP: Functions don't work
|
|
53
|
+
|
|
54
|
+
### 0.2.0 - Variables
|
|
55
|
+
|
|
56
|
+
- Basic variable modification `set number x = 20`
|
|
57
|
+
|
|
58
|
+
### 0.1.0 - Output
|
|
59
|
+
|
|
60
|
+
- Initial release
|
|
61
|
+
- Basic output `print("Hello World")`
|
|
62
|
+
- Basic variable declaration `number x = 10`
|
|
63
|
+
- Basic printing of variables `print(x)`
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 orangecatprog
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
# ocat_lang
|
|
2
|
-
|
|
3
|
-
To install dependencies:
|
|
4
|
-
|
|
5
|
-
```bash
|
|
6
|
-
bun install
|
|
7
|
-
```
|
|
8
|
-
|
|
9
|
-
To run:
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
bun run src/index.ts
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
This project was created using `bun init` in bun v1.1.33. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
|
|
1
|
+
# ocat_lang
|
|
2
|
+
|
|
3
|
+
To install dependencies:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bun install
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
To run:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bun run src/index.ts
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This project was created using `bun init` in bun v1.1.33. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
|
package/package.json
CHANGED
|
@@ -1,19 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "oclang",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A programming language",
|
|
5
|
-
"author": "LuisRG-L, Orange Cat",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"type": "module",
|
|
8
|
-
"module": "src/index.ts",
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "oclang",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"description": "A programming language",
|
|
5
|
+
"author": "LuisRG-L, Orange Cat",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"module": "src/index.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"ocat": "dist/cli/ocat/index.js",
|
|
11
|
+
"ocm": "dist/cli/ocm/index.js"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"test": "tsc && npm link --force"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/bun": "latest"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"typescript": "^5.0.0"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@types/node": "^25.0.3",
|
|
25
|
+
"chalk": "^5.6.2",
|
|
26
|
+
"chevrotain": "^11.0.3",
|
|
27
|
+
"commander": "^14.0.2",
|
|
28
|
+
"inquirer": "^13.1.0"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/cli/index.ts
ADDED
|
File without changes
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import readline from "readline";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
import { execute } from "../../../core/index.js";
|
|
4
|
+
|
|
5
|
+
export function repl() {
|
|
6
|
+
console.log(chalk.yellow("Orange Cat REPL v1.0.0"));
|
|
7
|
+
|
|
8
|
+
const rl = readline.createInterface({
|
|
9
|
+
input: process.stdin,
|
|
10
|
+
output: process.stdout,
|
|
11
|
+
prompt: chalk.cyan("ocat> "),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
rl.prompt();
|
|
15
|
+
|
|
16
|
+
rl.on("line", (line) => {
|
|
17
|
+
const input = line.trim();
|
|
18
|
+
|
|
19
|
+
// comandos internos
|
|
20
|
+
if (input === ".exit") {
|
|
21
|
+
rl.close();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (input === ".help") {
|
|
26
|
+
console.log(`
|
|
27
|
+
.exit Exit REPL
|
|
28
|
+
.clear Clear screen
|
|
29
|
+
`);
|
|
30
|
+
rl.prompt();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (input === ".clear") {
|
|
35
|
+
console.clear();
|
|
36
|
+
rl.prompt();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
execute(input);
|
|
41
|
+
|
|
42
|
+
rl.prompt();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
rl.on("close", () => {
|
|
46
|
+
console.log("\nBye!");
|
|
47
|
+
process.exit(0);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { execute } from "../../../core/index.js";
|
|
2
|
+
import { ExtensionError } from "../../../shared/manager/errors/io/extension.js";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import { FileDoesntExistError } from "../../../shared/manager/errors/io/file.js";
|
|
7
|
+
|
|
8
|
+
export interface RunCommandOptions {
|
|
9
|
+
force: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function runfile(file: string, options: RunCommandOptions) {
|
|
13
|
+
if (!file.endsWith(".ocat") && !options.force) {
|
|
14
|
+
new ExtensionError(
|
|
15
|
+
"File must be a .ocat file. Use -f to force execution with other extensions"
|
|
16
|
+
).throw();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (options.force) {
|
|
20
|
+
console.log(chalk.blue(`Running in force mode`));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if(!fs.existsSync(file)) {
|
|
24
|
+
new FileDoesntExistError(`File ${file} doesn't exist`)
|
|
25
|
+
.throw();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const fileText = fs.readFileSync(file, "utf8");
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
execute(fileText);
|
|
32
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
|
|
4
|
+
import { runfile } from "./commands/index.js";
|
|
5
|
+
import { repl } from "./commands/index.js";
|
|
6
|
+
|
|
7
|
+
const program = new Command();
|
|
8
|
+
|
|
9
|
+
program
|
|
10
|
+
.name("ocat")
|
|
11
|
+
.description("The Orange Cat language compiler")
|
|
12
|
+
.version("1.0.0");
|
|
13
|
+
|
|
14
|
+
program
|
|
15
|
+
.command("run")
|
|
16
|
+
.description("Run a file. This file must be a .ocat file")
|
|
17
|
+
.argument("<file>", "The file to run")
|
|
18
|
+
.option("-f, --force", "Force the execution with other extensions")
|
|
19
|
+
.action(runfile);
|
|
20
|
+
|
|
21
|
+
program
|
|
22
|
+
.command("inline")
|
|
23
|
+
.description("An REPL for the Orange Cat language")
|
|
24
|
+
.action(repl);
|
|
25
|
+
|
|
26
|
+
program.parse();
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import inquirer from "inquirer";
|
|
2
|
+
import chalk, { type ChalkInstance } from "chalk";
|
|
3
|
+
import { chalkText } from "../../utils/chalkText.js";
|
|
4
|
+
import { createProject } from "../../../project/index.js";
|
|
5
|
+
import { fromCamelToDash } from "../../../shared/utils/strformat.js";
|
|
6
|
+
|
|
7
|
+
interface Choice {
|
|
8
|
+
name: string;
|
|
9
|
+
color: ChalkInstance;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function init() {
|
|
13
|
+
const projectTypes: Choice[] = [{ name: "App", color: chalk.red }, { name: "Lib", color: chalk.yellow }];
|
|
14
|
+
|
|
15
|
+
const answers = await inquirer.prompt([
|
|
16
|
+
{
|
|
17
|
+
type: "input",
|
|
18
|
+
name: "name",
|
|
19
|
+
message: "How do you want to name your project?",
|
|
20
|
+
default: "myProject",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
type: "input",
|
|
24
|
+
name: "dir",
|
|
25
|
+
message: "Where do you want to create your project?",
|
|
26
|
+
default: (answers) => answers.name,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
type: "input",
|
|
30
|
+
name: "id",
|
|
31
|
+
message: "What is your project ID?",
|
|
32
|
+
default: (answers) => fromCamelToDash(answers.name),
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
type: "select",
|
|
36
|
+
name: "type",
|
|
37
|
+
message: "What type of project do you want to create?",
|
|
38
|
+
choices: projectTypes
|
|
39
|
+
.map((choice) => chalkText(choice.name, choice.color)),
|
|
40
|
+
}
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
createProject({
|
|
44
|
+
name: answers.name,
|
|
45
|
+
dir: answers.dir,
|
|
46
|
+
type: answers.type,
|
|
47
|
+
id: answers.id,
|
|
48
|
+
})
|
|
49
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { readJSON } from "../../../shared/io/json.js";
|
|
3
|
+
import { runfile } from "../../ocat/commands/index.js";
|
|
4
|
+
import * as context from "../../../shared/context/globalContext.js";
|
|
5
|
+
import { defaultLoggerConfig, LoggerService } from "../../../core/services/log.service.js";
|
|
6
|
+
|
|
7
|
+
export function run() {
|
|
8
|
+
const projectConfig = readJSON(path.join(".ocat", "config.json"));
|
|
9
|
+
|
|
10
|
+
context.set("isProject", true);
|
|
11
|
+
context.set("projectConfig", projectConfig);
|
|
12
|
+
context.set("services", {});
|
|
13
|
+
context.useObject("services", services => {
|
|
14
|
+
return {
|
|
15
|
+
log: new LoggerService(defaultLoggerConfig),
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
runfile(projectConfig.main, { force: false });
|
|
20
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
|
|
4
|
+
import { init, run } from "./commands/index.js";
|
|
5
|
+
|
|
6
|
+
const program = new Command();
|
|
7
|
+
|
|
8
|
+
program.name("ocm").description("The Orange Cat manager").version("1.0.0");
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
program
|
|
12
|
+
.command("initialize")
|
|
13
|
+
.alias("init")
|
|
14
|
+
.description("Initialize a new Orange Cat project")
|
|
15
|
+
.action(async () => { await init() });
|
|
16
|
+
|
|
17
|
+
program
|
|
18
|
+
.command("run")
|
|
19
|
+
.description("Run the project")
|
|
20
|
+
.action(run);
|
|
21
|
+
|
|
22
|
+
program.parse();
|
|
@@ -1,17 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AlreadyDeclaredFunctionError,
|
|
3
|
-
AlreadyDeclaredVariableError,
|
|
4
|
-
CantModifyConstError,
|
|
5
|
-
} from "../../shared/manager/errors/semantic/undefined/declared.js";
|
|
6
|
-
import {
|
|
7
|
-
UndeclaredFunctionError,
|
|
8
|
-
UndeclaredVariableError,
|
|
9
|
-
} from "../../shared/manager/errors/semantic/undefined/undeclared.js";
|
|
10
|
-
import { type CoreContext } from "../context/coreContext.js";
|
|
1
|
+
import { createCoreContext, type CoreContext } from "../context/coreContext.js";
|
|
11
2
|
import { ValueType } from "../../shared/models/value.js";
|
|
12
3
|
import { StatementKind } from "./types/base/index.js";
|
|
13
4
|
import type { AnyStatement, PrintStatement } from "./types/statements/index.js";
|
|
14
|
-
import { isNoSubstitutionTemplateLiteral } from "typescript";
|
|
15
5
|
|
|
16
6
|
export type BuildType = AnyStatement | null;
|
|
17
7
|
|
|
@@ -130,7 +120,8 @@ const functionStatement: FBuilder = (statement: any) => {
|
|
|
130
120
|
if (!idToken) return null;
|
|
131
121
|
const id = idToken.image;
|
|
132
122
|
|
|
133
|
-
const body = funcStmt
|
|
123
|
+
const body = buildAst(funcStmt) ;
|
|
124
|
+
|
|
134
125
|
return {
|
|
135
126
|
kind: StatementKind.FunctionStatement,
|
|
136
127
|
sourceInfo: {
|
|
@@ -142,7 +133,10 @@ const functionStatement: FBuilder = (statement: any) => {
|
|
|
142
133
|
endColumn: funcStmt.endColumn,
|
|
143
134
|
},
|
|
144
135
|
id,
|
|
145
|
-
|
|
136
|
+
func: {
|
|
137
|
+
body,
|
|
138
|
+
scope: createCoreContext(),
|
|
139
|
+
}
|
|
146
140
|
}
|
|
147
141
|
}
|
|
148
142
|
return null
|
|
@@ -165,8 +159,8 @@ const callStatement: FBuilder = (statement: any) => {
|
|
|
165
159
|
startColumn: callStmt.startColumn,
|
|
166
160
|
endColumn: callStmt.endColumn,
|
|
167
161
|
},
|
|
168
|
-
id
|
|
169
|
-
}
|
|
162
|
+
id,
|
|
163
|
+
};
|
|
170
164
|
}
|
|
171
|
-
return null
|
|
172
|
-
}
|
|
165
|
+
return null;
|
|
166
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./statement";
|
|
2
|
-
export * from "./source";
|
|
1
|
+
export * from "./statement.js";
|
|
2
|
+
export * from "./source.js";
|
|
@@ -1,16 +1,8 @@
|
|
|
1
1
|
import type { Statement, StatementKind } from "../base";
|
|
2
|
-
import type {
|
|
3
|
-
import type { PrintStatement } from "./printStatement";
|
|
4
|
-
import type { VariableStatement } from "./variableStatement";
|
|
5
|
-
|
|
6
|
-
export type AnyStatement =
|
|
7
|
-
| PrintStatement
|
|
8
|
-
| VariableStatement
|
|
9
|
-
| FunctionStatement
|
|
10
|
-
| CallStatement;
|
|
2
|
+
import type { Function } from "../../../../shared/models/func";
|
|
11
3
|
|
|
12
4
|
export interface FunctionStatement extends Statement {
|
|
13
5
|
kind: StatementKind.FunctionStatement;
|
|
14
6
|
id: string;
|
|
15
|
-
|
|
7
|
+
func: Function;
|
|
16
8
|
}
|
|
@@ -1,4 +1,16 @@
|
|
|
1
|
+
import type { FunctionStatement } from "./functionStatement";
|
|
2
|
+
import type { PrintStatement } from "./printStatement";
|
|
3
|
+
import type { VariableStatement } from "./variableStatement";
|
|
4
|
+
import type { CallStatement } from "./callStatement";
|
|
5
|
+
|
|
1
6
|
export * from "./printStatement";
|
|
2
7
|
export * from "./variableStatement";
|
|
3
8
|
export * from "./functionStatement";
|
|
4
|
-
export * from "./callStatement";
|
|
9
|
+
export * from "./callStatement";
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export type AnyStatement =
|
|
13
|
+
| PrintStatement
|
|
14
|
+
| VariableStatement
|
|
15
|
+
| FunctionStatement
|
|
16
|
+
| CallStatement;
|
package/src/core/index.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { ocatLexer } from "./lexer/tokens";
|
|
2
|
-
import { OcatParser } from "./parser/parser";
|
|
3
|
-
import { run } from "./runner/runner";
|
|
4
|
-
import { createCoreContext } from "./context/coreContext";
|
|
5
|
-
import { buildAst } from "./ast/astBuilder";
|
|
1
|
+
import { ocatLexer } from "./lexer/tokens.js";
|
|
2
|
+
import { OcatParser } from "./parser/parser.js";
|
|
3
|
+
import { run } from "./runner/runner.js";
|
|
4
|
+
import { createCoreContext } from "./context/coreContext.js";
|
|
5
|
+
import { buildAst } from "./ast/astBuilder.js";
|
|
6
|
+
import * as ctx from "../shared/context/globalContext.js";
|
|
7
|
+
import fs from "fs";
|
|
6
8
|
|
|
7
9
|
export function execute(code: string) {
|
|
8
10
|
const lexingResult = ocatLexer.tokenize(code);
|
|
@@ -10,7 +12,14 @@ export function execute(code: string) {
|
|
|
10
12
|
const parser = new OcatParser();
|
|
11
13
|
parser.input = lexingResult.tokens;
|
|
12
14
|
const cst = parser.program();
|
|
13
|
-
const ast = buildAst(cst
|
|
15
|
+
const ast = buildAst(cst);
|
|
16
|
+
|
|
17
|
+
process.on("exit", () => {
|
|
18
|
+
if (ctx.get("isProject")) {
|
|
19
|
+
const logs = ctx.get("services").log.toString();
|
|
20
|
+
fs.writeFileSync(".ocat/logs.txt", logs);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
14
23
|
|
|
15
24
|
const context = createCoreContext();
|
|
16
25
|
run(ast, context);
|
|
@@ -12,4 +12,6 @@ export const RightBracket = createToken({ name: "RightBracket", pattern: /\]/ })
|
|
|
12
12
|
export const LeftTag = createToken({ name: "LeftTag", pattern: /</ });
|
|
13
13
|
export const RightTag = createToken({ name: "RightTag", pattern: />/ });
|
|
14
14
|
|
|
15
|
-
export const
|
|
15
|
+
export const Comma = createToken({ name: "Comma", pattern: /,/ });
|
|
16
|
+
|
|
17
|
+
export const delimiters = [LeftParen, RightParen, LeftBrace, RightBrace, LeftBracket, RightBracket, LeftTag, RightTag, Comma];
|
|
@@ -7,11 +7,12 @@ import {
|
|
|
7
7
|
UndeclaredFunctionError,
|
|
8
8
|
UndeclaredVariableError,
|
|
9
9
|
} from "../../shared/manager/errors/semantic/undefined/undeclared.js";
|
|
10
|
-
import { type CoreContext } from "../context/coreContext.js";
|
|
10
|
+
import { createCoreContext, type CoreContext } from "../context/coreContext.js";
|
|
11
11
|
import { ValueType } from "../../shared/models/value.js";
|
|
12
12
|
import { solveString } from "./utils/string.js";
|
|
13
13
|
import type { AnyStatement, CallStatement, FunctionStatement, PrintStatement, VariableStatement } from "../ast/types/statements/index.js";
|
|
14
14
|
import { StatementKind } from "../ast/types/base/statement.js";
|
|
15
|
+
import * as ctx from "../../shared/context/globalContext.js";
|
|
15
16
|
|
|
16
17
|
export function run(ast: AnyStatement[], context: CoreContext) {
|
|
17
18
|
|
|
@@ -56,11 +57,13 @@ function printStatement(statement: PrintStatement, context: CoreContext) {
|
|
|
56
57
|
val = statement.value.value;
|
|
57
58
|
break;
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
+
const info = ctx.get("services").log;
|
|
61
|
+
info.info(val);
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
function variableStatement(statement: VariableStatement, context: CoreContext) {
|
|
63
65
|
const varId = statement.id;
|
|
66
|
+
|
|
64
67
|
if (statement.set) {
|
|
65
68
|
if (!context.variables[varId]) {
|
|
66
69
|
new UndeclaredVariableError(varId).throw(
|
|
@@ -110,9 +113,11 @@ function functionStatement(statement: FunctionStatement, context: CoreContext) {
|
|
|
110
113
|
new AlreadyDeclaredFunctionError(idToken).throw(statement.sourceInfo.startLine);
|
|
111
114
|
}
|
|
112
115
|
|
|
113
|
-
const body = statement.body ?? [];
|
|
116
|
+
const body = statement.func.body ?? [];
|
|
117
|
+
const scope = statement.func.scope;
|
|
114
118
|
context.functions[idToken] = {
|
|
115
119
|
body,
|
|
120
|
+
scope,
|
|
116
121
|
};
|
|
117
122
|
}
|
|
118
123
|
|
|
@@ -124,5 +129,5 @@ function callStatement(statement: CallStatement, context: CoreContext) {
|
|
|
124
129
|
if (!funcData) {
|
|
125
130
|
new UndeclaredFunctionError(funcId).throw(statement.sourceInfo.startLine);
|
|
126
131
|
}
|
|
127
|
-
run(funcData.body, context);
|
|
132
|
+
run(funcData.body, context.functions[funcId].scope);
|
|
128
133
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { OcatError } from "../../../shared/manager/errors/coreErrors";
|
|
2
|
-
import { UndeclaredVariableError } from "../../../shared/manager/errors/semantic/undefined/undeclared";
|
|
3
|
-
import type { CoreContext } from "../../context/coreContext";
|
|
1
|
+
import { OcatError } from "../../../shared/manager/errors/coreErrors.js";
|
|
2
|
+
import { UndeclaredVariableError } from "../../../shared/manager/errors/semantic/undefined/undeclared.js";
|
|
3
|
+
import type { CoreContext } from "../../context/coreContext.js";
|
|
4
4
|
|
|
5
5
|
export function solveString(input: string, context: CoreContext, err: (err: OcatError) => void): string {
|
|
6
6
|
let str: string = input;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export enum LogLevel {
|
|
2
|
+
Debug = "DEBUG",
|
|
3
|
+
Info = "INFO",
|
|
4
|
+
Warning = "WARNING",
|
|
5
|
+
Error = "ERROR",
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface Log {
|
|
9
|
+
message: string;
|
|
10
|
+
level: LogLevel;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface LoggerConfig {
|
|
14
|
+
interceptors: {
|
|
15
|
+
onLog: (message: string) => string;
|
|
16
|
+
level: LogLevel;
|
|
17
|
+
}[];
|
|
18
|
+
logs: LogLevel[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class LoggerService {
|
|
22
|
+
private logs: Log[] = [];
|
|
23
|
+
private config: LoggerConfig;
|
|
24
|
+
|
|
25
|
+
constructor(config: LoggerConfig) {
|
|
26
|
+
this.config = config;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
log (message: string, level: LogLevel = LogLevel.Info) {
|
|
30
|
+
this.logs.push({ message, level });
|
|
31
|
+
this.config.interceptors.forEach(interceptor => {
|
|
32
|
+
if (interceptor.level === level) {
|
|
33
|
+
message = interceptor.onLog(message);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
if (this.config.logs.includes(level)) {
|
|
37
|
+
console.log(message);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
debug (message: string) {
|
|
42
|
+
this.log(message, LogLevel.Debug);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
info (message: string) {
|
|
46
|
+
this.log(message, LogLevel.Info);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
warning (message: string) {
|
|
50
|
+
this.log(message, LogLevel.Warning);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
error (message: string) {
|
|
54
|
+
this.log(message, LogLevel.Error);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getLogs () {
|
|
58
|
+
return this.logs;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
pushInterceptor (interceptor: { onLog: (message: string) => string, level: LogLevel }) {
|
|
62
|
+
this.config.interceptors.push(interceptor);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
removeInterceptor (interceptor: { onLog: (message: string) => string, level: LogLevel }) {
|
|
66
|
+
this.config.interceptors = this.config.interceptors.filter(i => i !== interceptor);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
toString() {
|
|
70
|
+
return this.logs.map(log => `[${log.level}] ${log.message}`).join("\n");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const defaultLoggerConfig: LoggerConfig = {
|
|
75
|
+
interceptors: [],
|
|
76
|
+
logs: [LogLevel.Debug, LogLevel.Info],
|
|
77
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
import { ProjectType, type ProjectSettings } from "./models/project.js";
|
|
5
|
+
import { addIf } from '../shared/utils/objects.js';
|
|
6
|
+
|
|
7
|
+
export function createProject(ps: ProjectSettings) {
|
|
8
|
+
const projectPath = path.resolve(ps.dir);
|
|
9
|
+
|
|
10
|
+
fs.mkdirSync(path.join(projectPath, "src"), { recursive: true });
|
|
11
|
+
fs.mkdirSync(path.join(projectPath, "connection"), { recursive: true });
|
|
12
|
+
fs.mkdirSync(path.join(projectPath, ".ocat"), { recursive: true });
|
|
13
|
+
|
|
14
|
+
const mainFile = path.join(projectPath, "src", "main.ocat");
|
|
15
|
+
if (ps.type !== ProjectType.Lib) {
|
|
16
|
+
fs.writeFileSync(
|
|
17
|
+
mainFile,
|
|
18
|
+
'print("Hello World!")'
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
fs.writeFileSync(
|
|
23
|
+
path.join(projectPath, ".ocat", "config.json"),
|
|
24
|
+
JSON.stringify(
|
|
25
|
+
{
|
|
26
|
+
name: ps.name,
|
|
27
|
+
version: "1.0.0",
|
|
28
|
+
description: "",
|
|
29
|
+
id: ps.id,
|
|
30
|
+
...addIf(ps.type !== ProjectType.Lib, { main: path.join("src", "main.ocat") }),
|
|
31
|
+
type: ps.type.toLowerCase(),
|
|
32
|
+
},
|
|
33
|
+
null,
|
|
34
|
+
4
|
|
35
|
+
)
|
|
36
|
+
);
|
|
37
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ProjectType } from "../models/project.js";
|
|
2
|
+
|
|
3
|
+
export function toProjectType(type: string): ProjectType {
|
|
4
|
+
switch (type) {
|
|
5
|
+
case "App":
|
|
6
|
+
return ProjectType.App;
|
|
7
|
+
case "Lib":
|
|
8
|
+
return ProjectType.Lib;
|
|
9
|
+
default:
|
|
10
|
+
return ProjectType.App;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface Context {
|
|
2
|
+
[key: string]: any;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
let globalContext: Context = {};
|
|
6
|
+
|
|
7
|
+
export function get(key: string) {
|
|
8
|
+
return globalContext[key];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function set(key: string, value: any) {
|
|
12
|
+
globalContext[key] = value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function useArray(key: string, modify: (array: any[]) => any[]) {
|
|
16
|
+
set(key, modify(get(key)));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function useObject(key: string, modify: (object: any) => any) {
|
|
20
|
+
set(key, modify(get(key)));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function useContext(modify: (context: Context) => Context) {
|
|
24
|
+
globalContext = modify(globalContext);
|
|
25
|
+
}
|
|
@@ -1,24 +1,39 @@
|
|
|
1
1
|
import chalk, { type ChalkInstance } from "chalk";
|
|
2
|
+
import * as ctx from "../context/globalContext.js";
|
|
3
|
+
import type { LogLevel } from "../../core/services/log.service.js";
|
|
2
4
|
|
|
3
5
|
export class OcatManager {
|
|
4
6
|
public name: string;
|
|
5
7
|
public message: string;
|
|
6
8
|
public color: ChalkInstance;
|
|
9
|
+
public level: LogLevel;
|
|
7
10
|
|
|
8
|
-
constructor(name: string, color: ChalkInstance) {
|
|
11
|
+
constructor(name: string, color: ChalkInstance, level: LogLevel) {
|
|
9
12
|
this.name = name;
|
|
10
13
|
this.message = "";
|
|
11
14
|
this.color = color;
|
|
15
|
+
this.level = level;
|
|
12
16
|
}
|
|
13
17
|
|
|
14
18
|
toString(line: number | undefined = undefined): string {
|
|
15
|
-
return `${this.color.bold(this.name)}
|
|
16
|
-
line
|
|
19
|
+
return `${this.color.bold(this.name)}${
|
|
20
|
+
line ? chalk.gray(` at line ${line}`) : ""
|
|
17
21
|
}: ${this.color.italic(this.message)}`;
|
|
18
22
|
}
|
|
19
23
|
|
|
20
|
-
|
|
24
|
+
build(line: number | undefined = undefined): string {
|
|
25
|
+
return `${this.name}${line ?? ""}: ${this.message}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setMessage(message: string): this {
|
|
29
|
+
this.message = message;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
throw(line: number | undefined = undefined): never {
|
|
21
34
|
console.log(this.toString(line));
|
|
35
|
+
const log = ctx.get("services").log;
|
|
36
|
+
log.log(this.build(line), this.level);
|
|
22
37
|
process.exit(1);
|
|
23
38
|
}
|
|
24
39
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import chalk from "chalk";
|
|
2
|
-
import { OcatManager } from "../baseManager";
|
|
2
|
+
import { OcatManager } from "../baseManager.js";
|
|
3
|
+
import { LogLevel } from "../../../core/services/log.service.js";
|
|
3
4
|
|
|
4
5
|
export class OcatError extends OcatManager {
|
|
5
6
|
constructor(name: string) {
|
|
6
|
-
super(name, chalk.red);
|
|
7
|
+
super(name, chalk.red, LogLevel.Error);
|
|
7
8
|
}
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
-
export * from "./api";
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { ContextType } from "../../../../../core/context/contextType";
|
|
2
|
-
import { OcatError } from "../../coreErrors";
|
|
1
|
+
import { ContextType } from "../../../../../core/context/contextType.js";
|
|
2
|
+
import { OcatError } from "../../coreErrors.js";
|
|
3
3
|
|
|
4
4
|
export class AlreadyDeclaredError extends OcatError {
|
|
5
5
|
constructor(varName: string, type: ContextType) {
|
|
6
6
|
super(`Already declared ${type.toLowerCase()}`);
|
|
7
|
-
this.
|
|
7
|
+
this.setMessage(`${type} ${varName} is already declared`);
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
|
|
@@ -23,6 +23,6 @@ export class AlreadyDeclaredFunctionError extends AlreadyDeclaredError {
|
|
|
23
23
|
export class CantModifyConstError extends OcatError {
|
|
24
24
|
constructor(varName: string) {
|
|
25
25
|
super(`Can't modify const variable`);
|
|
26
|
-
this.
|
|
26
|
+
this.setMessage(`Variable ${varName} is const and can't be modified`);
|
|
27
27
|
}
|
|
28
28
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { ContextType } from "../../../../../core/context/contextType";
|
|
2
|
-
import { OcatError } from "../../coreErrors";
|
|
1
|
+
import { ContextType } from "../../../../../core/context/contextType.js";
|
|
2
|
+
import { OcatError } from "../../coreErrors.js";
|
|
3
3
|
|
|
4
4
|
export class UndeclaredError extends OcatError {
|
|
5
5
|
constructor(varName: string, type: ContextType) {
|
|
6
6
|
super(`Undeclared ${type.toLowerCase()}`);
|
|
7
|
-
this.
|
|
7
|
+
this.setMessage(`${type} ${varName} is not declared`);
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import chalk from "chalk";
|
|
2
2
|
import { OcatManager } from "../baseManager";
|
|
3
|
+
import { LogLevel } from "../../../core/services/log.service";
|
|
3
4
|
|
|
4
5
|
export class OcatWarning extends OcatManager {
|
|
5
6
|
constructor(name: string) {
|
|
6
|
-
super(name, chalk.yellow);
|
|
7
|
+
super(name, chalk.yellow, LogLevel.Warning);
|
|
7
8
|
}
|
|
8
9
|
}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import type { AnyStatement } from "../../core/ast/types/statements/index.js";
|
|
2
|
+
import type { CoreContext } from "../../core/context/coreContext.js";
|
|
3
|
+
import type { ValueType } from "./value.js";
|
|
4
|
+
import type { Variable } from "./var.js";
|
|
5
|
+
|
|
6
|
+
export interface Param {
|
|
7
|
+
type: ValueType;
|
|
8
|
+
name: string;
|
|
9
|
+
}
|
|
2
10
|
|
|
3
11
|
export interface Function {
|
|
4
12
|
body: AnyStatement[];
|
|
13
|
+
scope: CoreContext;
|
|
5
14
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function fromCamelToDash(str: string): string {
|
|
2
|
+
return str.replace(/([A-Z])/g, (match) => "-" + match.toLowerCase());
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function fromDashToCamel(str: string): string {
|
|
6
|
+
return str.replace(/-([a-z])/g, (match) => match[1].toUpperCase());
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function fromCamelToSnake(str: string): string {
|
|
10
|
+
return str.replace(/([A-Z])/g, (match) => "_" + match.toLowerCase());
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function fromSnakeToCamel(str: string): string {
|
|
14
|
+
return str.replace(/_([a-z])/g, (match) => match[1].toUpperCase());
|
|
15
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Enable latest features
|
|
4
|
+
"lib": ["ESNext", "DOM"],
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"module": "ESNext",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
|
|
11
|
+
// Bundler mode
|
|
12
|
+
"moduleResolution": "bundler",
|
|
13
|
+
"verbatimModuleSyntax": true,
|
|
14
|
+
|
|
15
|
+
// Best practices
|
|
16
|
+
"strict": true,
|
|
17
|
+
"skipLibCheck": true,
|
|
18
|
+
"noFallthroughCasesInSwitch": true,
|
|
19
|
+
|
|
20
|
+
// Some stricter flags (disabled by default)
|
|
21
|
+
"noUnusedLocals": false,
|
|
22
|
+
"noUnusedParameters": false,
|
|
23
|
+
"noPropertyAccessFromIndexSignature": false,
|
|
24
|
+
|
|
25
|
+
// Output
|
|
26
|
+
"outDir": "dist"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.ts
DELETED