Skip to content

Commit

Permalink
Implement methods for all primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandertrefz committed Feb 18, 2024
1 parent 184162c commit 7a51123
Show file tree
Hide file tree
Showing 29 changed files with 8,194 additions and 2,451 deletions.
28 changes: 21 additions & 7 deletions src/enricher/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fractionType from "./types/Fraction"
import integerType from "./types/Integer"
import listType from "./types/List"
import nothingType from "./types/Nothing"
import recordType from "./types/Record"
import stringType from "./types/String"

export function resolveType(
Expand Down Expand Up @@ -632,15 +633,24 @@ export function resolveMethodLookupType(
scope: enricher.Scope,
): common.MethodType {
let baseType = resolveMethodLookupBaseType(node.base, scope)
let result = baseType.methods[node.member.content]

if (result == null) {
throw new Error(
`Could not resolve Method ${node.member.content} on Type of Expression at ${node.base.position.start.line}:${node.base.position.start.column}.`,
)
}
// Check wether the called Method exists on the Record Base Type
if (
baseType.definition.type === "Record" &&
recordType.methods[node.member.content]
) {
return recordType.methods[node.member.content]
} else {
let result = baseType.methods[node.member.content]

return result
if (result == null) {
throw new Error(
`Could not resolve Method ${node.member.content} on Type of Expression at ${node.base.position.start.line}:${node.base.position.start.column}.`,
)
}

return result
}
}

export function resolveGenericFunctionDefinitionType(
Expand Down Expand Up @@ -893,6 +903,8 @@ export function resolveMethodLookupBaseType(
`Could not resolve Member on a Type at ${node.position.start.line}:${node.position.start.column}.\nLeft hand side is Nothing.`,
)
}
case "Record":
return recordType
default:
throw new Error(
`Could not resolve Member on a Type at ${node.position.start.line}:${node.position.start.column}.`,
Expand Down Expand Up @@ -1062,6 +1074,8 @@ export function resolvePrimitiveTypeType(
return booleanType
case "String":
return stringType
case "Record":
return recordType
}
}

Expand Down
32 changes: 30 additions & 2 deletions src/enricher/types/Boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ const type: common.TypeType = {
name: "Boolean",
definition: { type: "Primitive", primitive: "Boolean" },
methods: {
negate: {
type: "SimpleMethod",
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Boolean" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
is: {
type: "SimpleMethod",
parameterTypes: [
Expand All @@ -19,7 +29,7 @@ const type: common.TypeType = {
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
isnt: {
isNot: {
type: "SimpleMethod",
parameterTypes: [
{
Expand All @@ -33,13 +43,31 @@ const type: common.TypeType = {
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
negate: {
and: {
type: "SimpleMethod",
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Boolean" },
},
{
name: null,
type: { type: "Primitive", primitive: "Boolean" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
or: {
type: "SimpleMethod",
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Boolean" },
},
{
name: null,
type: { type: "Primitive", primitive: "Boolean" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
Expand Down
179 changes: 177 additions & 2 deletions src/enricher/types/Fraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,53 @@ const type: common.TypeType = {
name: "Fraction",
definition: { type: "Primitive", primitive: "Fraction" },
methods: {
of: {
type: "StaticMethod",
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Integer" },
},
{
name: "over",
type: { type: "Primitive", primitive: "Integer" },
},
],
returnType: { type: "Primitive", primitive: "Fraction" },
},

is: {
type: "SimpleMethod",

parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},

isNot: {
type: "SimpleMethod",

parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},

add: {
type: "OverloadedMethod",
overloads: [
Expand Down Expand Up @@ -69,7 +116,7 @@ const type: common.TypeType = {
],
},

divide: {
divideBy: {
type: "OverloadedMethod",
overloads: [
{
Expand Down Expand Up @@ -101,7 +148,7 @@ const type: common.TypeType = {
],
},

multiply: {
multiplyWith: {
type: "OverloadedMethod",
overloads: [
{
Expand Down Expand Up @@ -133,6 +180,134 @@ const type: common.TypeType = {
],
},

isLessThan: {
type: "OverloadedMethod",
overloads: [
{
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
{
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
{
name: null,
type: { type: "Primitive", primitive: "Integer" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
],
},

isLessThanOrEqualTo: {
type: "OverloadedMethod",
overloads: [
{
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
{
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
{
name: null,
type: { type: "Primitive", primitive: "Integer" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
],
},

isGreaterThan: {
type: "OverloadedMethod",
overloads: [
{
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
{
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
{
name: null,
type: { type: "Primitive", primitive: "Integer" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
],
},

isGreaterThanOrEqualTo: {
type: "OverloadedMethod",
overloads: [
{
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
{
parameterTypes: [
{
name: null,
type: { type: "Primitive", primitive: "Fraction" },
},
{
name: null,
type: { type: "Primitive", primitive: "Integer" },
},
],
returnType: { type: "Primitive", primitive: "Boolean" },
},
],
},

toString: {
type: "OverloadedMethod",
overloads: [
Expand Down
Loading

0 comments on commit 7a51123

Please sign in to comment.