Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/david518yang/Lang
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattmart42 committed May 2, 2024
2 parents b227cef + 0c5b581 commit c5b0348
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 237 deletions.
6 changes: 6 additions & 0 deletions examples/calls.MODE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
func f(x:int, y:int): void {
print x + y;
}
f(3, 4);
print f(3, 4);

2 changes: 1 addition & 1 deletion src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export default function analyze(match) {
},

Stmt_call(call, _semicolon) {
return call.rep()
return core.callStatement(call.rep())
},

BreakStmt(breakKeyword, _semicolon) {
Expand Down
22 changes: 15 additions & 7 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +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 const voidType = { kind: 'VoidType' }

export function classType(name, fields) {
return { kind: 'ClassType', name, fields }
Expand Down Expand Up @@ -76,10 +76,9 @@ export function forStatement(iterator, collection, body) {
}

export function forRangeStatement(iterator, start, op, end, body) {
return { kind: 'forRangeStatement', iterator, start, op, end, body };
return { kind: 'ForRangeStatement', iterator, start, op, end, body }
}


export function returnStatement(expression) {
return { kind: 'ReturnStatement', expression }
}
Expand Down Expand Up @@ -117,13 +116,22 @@ export function emptyOptional(baseType) {
}

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

export function memberExpression(object, op, field) {
return { kind: 'MemberExpression', object, op, field, type: field.type }
}

export function callStatement(call) {
return { kind: 'CallStatement', call }
}

export function functionCall(callee, args) {
return { kind: 'FunctionCall', callee, args, type: callee.type.returnType }
}
Expand All @@ -143,11 +151,11 @@ export const standardLibrary = Object.freeze({
boolean: boolType,
string: stringType,
any: anyType,
print: fun("print", anyToVoidType),
print: fun('print', anyToVoidType),
π: variable('π', true, floatType),
exp: fun('exp', floatToFloatType),
ln: fun('ln', floatToFloatType),
void: voidType
void: voidType,
})

String.prototype.type = stringType
Expand Down

0 comments on commit c5b0348

Please sign in to comment.