oclang 0.3.0 → 1.2.2
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 +15 -0
- package/CHANGELOG.md +50 -10
- package/LICENSE +21 -0
- package/README.md +15 -15
- package/dist/cli/index.js +1 -0
- package/dist/cli/ocat/commands/index.js +2 -0
- package/dist/cli/ocat/commands/repl.js +39 -0
- package/dist/cli/ocat/commands/run.js +19 -0
- package/dist/cli/ocat/index.js +20 -0
- package/dist/cli/ocm/commands/index.js +2 -0
- package/dist/cli/ocm/commands/init.js +41 -0
- package/dist/cli/ocm/commands/run.js +17 -0
- package/dist/cli/ocm/index.js +15 -0
- package/dist/cli/utils/chalkText.js +6 -0
- package/dist/core/ast/astBuilder.js +150 -0
- package/dist/core/ast/types/base/index.js +2 -0
- package/dist/core/ast/types/base/source.js +1 -0
- package/dist/core/ast/types/base/statement.js +7 -0
- package/dist/core/ast/types/statements/callStatement.js +1 -0
- package/dist/core/ast/types/statements/functionStatement.js +1 -0
- package/dist/core/ast/types/statements/index.js +4 -0
- package/dist/core/ast/types/statements/printStatement.js +1 -0
- package/dist/core/ast/types/statements/variableStatement.js +1 -0
- package/dist/core/context/contextType.js +5 -0
- package/dist/core/context/coreContext.js +6 -0
- package/dist/core/index.js +22 -0
- package/dist/core/lexer/tokens/attrs.js +6 -0
- package/dist/core/lexer/tokens/comments.js +12 -0
- package/dist/core/lexer/tokens/delimiters.js +11 -0
- package/dist/core/lexer/tokens/function.js +10 -0
- package/dist/core/lexer/tokens/ignored.js +3 -0
- package/dist/core/lexer/tokens/index.js +29 -0
- package/dist/core/lexer/tokens/keywords.js +3 -0
- package/dist/core/lexer/tokens/literals.js +14 -0
- package/dist/core/lexer/tokens/operators.js +3 -0
- package/dist/core/lexer/tokens/vars.js +12 -0
- package/dist/core/lexer/tokens.js +3 -0
- package/dist/core/parser/parser.js +84 -0
- package/dist/core/runner/runner.js +108 -0
- package/dist/core/runner/utils/string.js +14 -0
- package/dist/core/services/log.service.js +53 -0
- package/dist/project/index.js +22 -0
- package/dist/project/models/project.js +5 -0
- package/dist/project/utils/project.js +11 -0
- package/dist/shared/context/globalContext.js +16 -0
- package/dist/shared/io/json.js +7 -0
- package/dist/shared/manager/baseManager.js +30 -0
- package/dist/shared/manager/errors/coreErrors.js +8 -0
- package/dist/shared/manager/errors/io/extension.js +7 -0
- package/dist/shared/manager/errors/io/file.js +7 -0
- package/dist/shared/manager/errors/semantic/undefined/declared.js +24 -0
- package/dist/shared/manager/errors/semantic/undefined/undeclared.js +18 -0
- package/dist/shared/manager/errors/syntax/syntaxErrors.js +7 -0
- package/dist/shared/manager/errors/syntax/token.js +11 -0
- package/dist/shared/manager/warn/coreWarnings.js +8 -0
- package/dist/shared/models/func.js +1 -0
- package/dist/shared/models/value.js +7 -0
- package/dist/shared/models/var.js +1 -0
- package/dist/shared/utils/objects.js +6 -0
- package/dist/shared/utils/strformat.js +12 -0
- package/package.json +14 -3
- 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
|
@@ -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
|
}
|