Skip to content

Commit

Permalink
Add #error, #error function, #error macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiriVulpes committed Dec 15, 2024
1 parent fafecfa commit f4ca4d3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/chc/read/consume/consumeMacroUseOptional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import macroContinue from "./macro/macroContinue"
import macroDebug from "./macro/macroDebug"
import type { ChiriDo } from "./macro/macroDo"
import type { ChiriEach } from "./macro/macroEach"
import macroError from "./macro/macroError"
import macroExport from "./macro/macroExport"
import type { ChiriFontFace } from "./macro/macroFontFace"
import macroFontFace from "./macro/macroFontFace"
Expand Down Expand Up @@ -93,6 +94,7 @@ export default async function (reader: ChiriReader, ...args: any[]): Promise<Mac
?? await macroImportCSS.consumeOptional(reader, ...context)
?? await macroImport.consumeOptional(reader, ...context)
?? await macroDebug.consumeOptional(reader, ...context)
?? await macroError.consumeOptional(reader, ...context)
?? await macroMacroDeclaration.consumeOptional(reader, ...context)
?? await macroFunctionDeclaration.consumeOptional(reader, ...context)
?? await macroShorthand.consumeOptional(reader, ...context)
Expand Down
16 changes: 16 additions & 0 deletions src/chc/read/consume/macro/macroError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import typeBool from "../../../type/typeBool"
import makeLiteralBool from "../../factory/makeLiteralBool"
import type { ChiriMacroUse } from "../consumeMacroUseOptional"
import MacroConstruct from "./MacroConstruct"

export default MacroConstruct("error")
.body("text")
.parameter("function", typeBool.type, makeLiteralBool(false))
.parameter("macro", typeBool.type, makeLiteralBool(false))
.consume(({ reader, assignments, body, position }): ChiriMacroUse => ({
type: "macro-use",
name: { type: "word", value: "error", position: { file: "internal", line: 0, column: 0 } },
assignments,
content: body,
position,
}))
9 changes: 9 additions & 0 deletions src/chc/write/ChiriCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ChiriType } from "../type/ChiriType"
import ChiriTypeManager from "../type/ChiriTypeManager"
import type { BodyVariableContext, BodyVariableContexts } from "../type/typeBody"
import typeString from "../type/typeString"
import _ from "../util/_"
import type { ComponentStateSpecial } from "../util/componentStates"
import getFunctionParameters from "../util/getFunctionParameters"
import relToCwd from "../util/relToCwd"
Expand Down Expand Up @@ -1301,6 +1302,14 @@ function ChiriCompiler (ast: ChiriAST, dest: string): ChiriCompiler {
logLine(statement.position, ansi.label + "debug" + (lines.length === 1 ? " - " : "") + ansi.reset + (lines.length <= 1 ? "" : "\n") + lines.join("\n"), false, false)
return true
}
case "error": {
const lines = compileStatements(statement.content, undefined, compileText)
const position = _
?? (!resolveExpression(compiler, statement.assignments.function) ? undefined : blocks.findLast(block => block.type === "function-call")?.position)
?? (!resolveExpression(compiler, statement.assignments.macro) ? undefined : blocks.findLast(block => block.type === "macro-use")?.position)
?? statement.position
throw error(position, (lines.length <= 1 ? "" : "\n") + lines.join("\n"))
}
}

const fn = getMacro(statement.name.value, statement.position)
Expand Down

0 comments on commit f4ca4d3

Please sign in to comment.