Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimberly-Kubo committed Apr 17, 2024
2 parents d1eaf68 + 3bfd469 commit f889f4d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export default function analyze(match) {
must(e.type === BOOLEAN, "Expected a boolean", at);
}

function mustHaveListType(e, at) {
must(e.type?.kind === "ListType", "Expected a list", at);
}

// function mustHaveAClassType(e, at) {
// must(e.type?.kind === "ClassType", "Expected a class", at);
// }
Expand Down Expand Up @@ -122,13 +126,13 @@ export default function analyze(match) {
case "BoolType":
return "boolean";
//case "ClassType":
//return type.name;
//return type.name;
case "FunctionType":
const paramTypes = type.paramTypes.map(typeDescription).join(", ");
const returnType = typeDescription(type.returnType);
return `(${paramTypes})->${returnType}`;
//case "ListType":
//return `[${typeDescription(type.baseType)}]`;
//return `[${typeDescription(type.baseType)}]`;
}
}

Expand Down Expand Up @@ -456,8 +460,8 @@ export default function analyze(match) {

return list._node.matchLength > 0 ? core.listType(baseType) : baseType;
},
id(_first, _rest){
return this.sourceString
id(_first, _rest) {
return this.sourceString;
},
true(_) {
return true;
Expand Down
3 changes: 3 additions & 0 deletions src/blvd.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ blvd {
MemberExp = given
Params = ListOf<Param, ","> ","?
Param = Type id



// EXTRA FUNCTIONS
RangeFunc = range from Exp "," Exp
Expand All @@ -58,6 +60,7 @@ blvd {
| Exp6
Exp6 = "(" Exp ")" --parens
| "["ListOf<Exp, ","> "]" --listexp
| id "[" num "]" --subsript
| message
| (true | false)
| Exp6 "(" ListOf<Exp, ","> ")" --call
Expand Down
12 changes: 6 additions & 6 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const boolType = { kind: "BoolType" };
export const numberType = { kind: "NumberType" };
export const stringType = { kind: "StringType " };
//export function customType(type) {
//return { kind: "CustomType", id: type };
//return { kind: "CustomType", id: type };
//}
export function emptyStatement() {
return { kind: "EmptyStatement" };
Expand Down Expand Up @@ -64,13 +64,13 @@ export function functionDeclaration(name, parameters, returnType, body) {
// return { kind: "ClassDeclaration", name, fields, methods, constructor, functions };
// }
//export function field(type, name) {
//return { kind: "Field", type, name };
//return { kind: "Field", type, name };
//}
// export function constructor(parameters, body) {
// return { kind: "Constructor", parameters, body };
// }
//export function memberExpression(object, id) {
//return { kind: "MemberExpression", object, id };
//return { kind: "MemberExpression", object, id };
//}
export function rangeFunction(lowerbound, upperbound) {
return { kind: "RangeFunction", lowerbound, upperbound };
Expand All @@ -81,12 +81,12 @@ export function binaryExpression(op, left, right, type) {
export function unaryExpression(operand) {
return { kind: "UnaryExpression", operand };
}
export function subscript(list, index) {
return { kind: "subscript", list, index, type: list.type.baseType };
}
export function listExpression(elements) {
return { kind: "ListExpression", elements };
}
//export function subscriptExpression(array, index) {
//return { kind: "SubscriptExpression", array, index };
//}
export function call(callee, args) {
return { kind: "Call", callee, args, type: callee.type.returnType };
}
Expand Down
17 changes: 17 additions & 0 deletions test/analyzer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ const semanticChecks = [
FIN`,
],
/*
[
"list indexing example",
`PROLOGUE
CAST string list shows as ["Martin", "Succession", "致我们单纯的小美好"]--
say(shows[1])--
END OF PROLOGUE
ACT 1
END OF ACT
EPILOGUE
FIN`,
],
*/
[
"increment and decrement",
`PROLOGUE
Expand Down
4 changes: 4 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ const syntaxChecks = [
"class declarations",
`PROLOGUE\n STAGE Movie:\n string a--\n string b--\n EXIT STAGE\n\n END OF PROLOGUE\n\n ACT 1\n\n END OF ACT\n\n EPILOGUE\n\n FIN\n`,
],
[
"list indexing",
`PROLOGUE\n STAGE Movie:\n string a--\n string b--\n EXIT STAGE\n\n END OF PROLOGUE\n\n ACT 1\n\n END OF ACT\n\n EPILOGUE\n\n FIN\n`,
],
];

const syntaxErrors = [
Expand Down

0 comments on commit f889f4d

Please sign in to comment.