Skip to content

Commit

Permalink
subscripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattmart42 committed Apr 28, 2024
1 parent 8abc42f commit 40628e3
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/MODE.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ MODE {
Primary = Primary_id "(" ListOf<Exp, ","> ")" --call
| float --num
| int --int
| Primary ("[" | "?[") Exp "]" --subscript
| Primary "." id --member
| id --id
| string ~mut --string
Expand Down
12 changes: 6 additions & 6 deletions src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,12 @@ export default function analyze(match) {
return expression.rep()
},

// Primary_subscript(exp1, _open, exp2, _close) {
// const [array, subscript] = [exp1.rep(), exp2.rep()]
// mustHaveAnArrayType(array, { at: exp1 })
// mustHaveIntegerType(subscript, { at: exp2 })
// return core.subscript(array, subscript)
// },
Primary_subscript(exp1, _open, exp2, _close) {
const [array, subscript] = [exp1.rep(), exp2.rep()]
mustHaveAnArrayType(array, { at: exp1 })
mustHaveIntegerType(subscript, { at: exp2 })
return core.subscript(array, subscript)
},

Primary_member(exp, _dot, id) {
const object = exp.rep()
Expand Down
7 changes: 7 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const intType = { kind: 'IntType' }
export const floatType = { kind: 'FloatType' }
export const stringType = { kind: 'StringType' }
export const anyType = { kind: 'AnyType' }
export const voidType = { kind: "VoidType" }

export function classType(name, fields) {
return { kind: 'ClassType', name, fields }
Expand Down Expand Up @@ -114,6 +115,10 @@ export function emptyOptional(baseType) {
return { kind: 'EmptyOptional', baseType, type: optionalType(baseType) }
}

export function subscript(array, index) {
return { kind: "SubscriptExpression", array, index, type: array.type.baseType }
}

export function memberExpression(object, op, field) {
return { kind: 'MemberExpression', object, op, field, type: field.type }
}
Expand All @@ -129,6 +134,7 @@ export function constructorCall(callee, args) {
const floatToFloatType = functionType([floatType], floatType)
const floatFloatToFloatType = functionType([floatType, floatType], floatType)
const stringToIntsType = functionType([stringType], arrayType(intType))
const anyToVoidType = functionType([anyType], voidType)

export const standardLibrary = Object.freeze({
int: intType,
Expand All @@ -139,6 +145,7 @@ export const standardLibrary = Object.freeze({
π: variable('π', true, floatType),
exp: fun('exp', floatToFloatType),
ln: fun('ln', floatToFloatType),
void: voidType
})

String.prototype.type = stringType
Expand Down
9 changes: 0 additions & 9 deletions src/optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,6 @@ const optimizers = {
s.body = s.body.flatMap(optimize)
return s
},
RepeatStatement(s) {
s.count = optimize(s.count)
if (s.count === 0) {
// repeat 0 times is a no-op
return []
}
s.body = s.body.flatMap(optimize)
return s
},
ForRangeStatement(s) {
s.iterator = optimize(s.iterator)
s.low = optimize(s.low)
Expand Down
2 changes: 1 addition & 1 deletion test/analyzer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const semanticChecks = [
['class declaration', 'class Dog { name:string; age:int;};'],
// ['member exp', 'struct S {x: int} let y = S(1);print(y.x);'],
// ['optional member exp', 'struct S {x: int} let y = some S(1);print(y?.x);'],
// ['subscript exp', 'let a=[1,2];print(a[0]);'],
['subscript exp', 'auto a=[1,2];print a[0];'],
// ['array of struct', 'struct S{} let x=[S(), S()];'],
// ['struct of arrays and opts', 'struct S{x: [int] y: string??}'],
// ['assigned functions', 'function f() {}\nlet g = f;g = f;'],
Expand Down
10 changes: 6 additions & 4 deletions test/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,23 @@ const fixtures = [
source: `
auto a = [true, false, true];
auto b = [10, 20, 30];
auto c = int[]()
print a[1] b[0];
auto c = int[]();
print a[1];
print b[1];
`,
expected: dedent`
let a_1 = [true,false,true];
let b_2 = [10,20,30];
let c_3 = [];
console.log(a_1[1], b_2[0]);
console.log(a_1[1]);
console.log(b_2[1]);
`,
},
// Not synactically correct yet - member operator not implemented
{
name: "classes",
source: `
class S { x: int }
class S { x: int; };
auto x = S(3);
print(x.x);
`,
Expand Down
1 change: 1 addition & 0 deletions test/optimizer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const tests = [
["optimizes left conditional true", core.conditional(true, 55, 89), 55],
["optimizes left conditional false", core.conditional(false, 55, 89), 89],
["optimizes in functions", program([intFun(return1p1)]), program([intFun(return2)])],
["optimizes in subscripts", sub(a, onePlusTwo), sub(a, 3)],
["optimizes in array literals", array(0, onePlusTwo, 9), array(0, 3, 9)],
["optimizes in arguments", callIdentity([times(3, 5)]), callIdentity([15])],
[
Expand Down

0 comments on commit 40628e3

Please sign in to comment.