Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
[#595] First class types
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhue committed Dec 22, 2021
1 parent 98245df commit d065d3d
Show file tree
Hide file tree
Showing 10 changed files with 866 additions and 717 deletions.
4 changes: 2 additions & 2 deletions common/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const declaration_member = <RuleName extends string>(
field('name', alias($.js_identifier, $.identifier_pattern_name)),
),
),
'::',
field('type', $._type),
':',
field('type', $._term),
)

export const js_identifier = () => token(seq(JS_ALPHA, repeat(JS_ALPHANUMERIC)))
37 changes: 19 additions & 18 deletions common/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@ export enum Prec {
Biconditional = 3,
TaggedType = 4,
Implication = 4,
Or = 5,
And = 6,
Equality = 7,
Order = 8,
Mod = 9,
Sum = 10,
Product = 11,
Exponentiation = 12,
Not = 13,
InfixApplication = 14,
Application = 15,
Term = 16,
Pattern = 16,
Access = 17,
Pipeline = 18,
ParametricTypeInstance = 19,
Argument = 20,
Hole = 21,
Difference = 5,
Or = 6,
And = 7,
Equality = 8,
Order = 9,
Mod = 10,
Sum = 11,
Product = 12,
Exponentiation = 13,
Not = 14,
InfixApplication = 15,
Application = 16,
Term = 17,
Pattern = 17,
Access = 18,
Pipeline = 19,
ParametricTypeInstance = 20,
Argument = 21,
Hole = 22,
}

export enum Dialect {
Expand Down
123 changes: 104 additions & 19 deletions common/terms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IDENTIFIER, OPERATOR } from './constants'
import { IDENTIFIER, OPERATOR, TYPE } from './constants'
import {
buildBlock,
buildGenericType,
Expand All @@ -9,6 +9,7 @@ import {
buildTypeConstraint,
buildTypeDeclaration,
commaSep1,
sep1,
} from './util'
import { Prec } from './enums'

Expand All @@ -17,7 +18,7 @@ export const _term = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
Prec.Term,
choice(
$.block,
$.abstraction,
$.function,
$.application,
$.infix_application,
$._section,
Expand All @@ -36,9 +37,14 @@ export const _term = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
$.list_comprehension,
$.parametric_type_instance,
$.pure,
$.type_alias,
$.type_hint,
$.hole,
$.type_hint,
$.data,
$.function_type,
$.optional_type,
$.map_type,
$.keyof_type,
$.type,
$.identifier,
$._literal,
$.group,
Expand Down Expand Up @@ -72,8 +78,8 @@ export const class_member = <RuleName extends string>(
) =>
seq(
field('name', alias($.identifier, $.identifier_pattern_name)),
'::',
field('type', $._type),
':',
field('type', $._term),
)

export const instance = <RuleName extends string>(
Expand All @@ -95,7 +101,7 @@ export const argument = <RuleName extends string>(
choice(field('placeholder', '?'), field('value', $._element)),
)

export const abstraction = <RuleName extends string>(
export const function_ = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.left(
Expand Down Expand Up @@ -255,6 +261,14 @@ export const infix_application = <RuleName extends string>(
field('right', $._term),
),
),
prec.left(
Prec.And,
seq(
field('left', $._term),
field('name', alias('&', $.identifier)),
field('right', $._term),
),
),
prec.left(
Prec.Or,
seq(
Expand All @@ -263,6 +277,22 @@ export const infix_application = <RuleName extends string>(
field('right', $._term),
),
),
prec.left(
Prec.Or,
seq(
field('left', $._term),
field('name', alias('|', $.identifier)),
field('right', $._term),
),
),
prec.left(
Prec.Difference,
seq(
field('left', $._term),
field('name', alias('\\', $.identifier)),
field('right', $._term),
),
),
prec.left(
Prec.Implication,
seq(
Expand Down Expand Up @@ -376,6 +406,7 @@ export const return_ = <RuleName extends string>($: GrammarSymbols<RuleName>) =>

export const ternary = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
prec.right(
20,
seq(
field('condition', $._term),
'?',
Expand Down Expand Up @@ -455,36 +486,90 @@ export const parametric_type_instance = <RuleName extends string>(
Prec.ParametricTypeInstance,
seq(
field('name', $._term),
buildGenericType('typeArgument', $.parametric_type),
buildGenericType('typeArgument', $._term),
),
)

export const pure = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
prec.right(seq('pure', field('value', $._term)))

export const type_alias = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.right(seq('type', buildTypeDeclaration($), '=', field('type', $._type)))
export const hole = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
prec(
Prec.Hole,
seq('?', field('name', alias($.identifier, $.identifier_pattern_name))),
)

export const type_hint = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.left(
Prec.TypeHint,
seq(field('value', $._term), 'as', field('type', $._type)),
seq(field('value', $._term), 'as', field('type', $._term)),
)

export const hole = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
prec(
Prec.Hole,
seq('?', field('name', alias($.identifier, $.identifier_pattern_name))),
export const data = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.right(seq('data', buildTypeDeclaration($), '=', sep1('|')(field('constructor', $.data_constructor))))

export const data_constructor = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.right(seq(
field(
'name',
alias($._identifier_without_operators, $.identifier_pattern_name),
),
optional(field('type', $._term)),
))

export const type_variable_declaration = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.left(
seq(
field('name', alias($.type, $.type_variable_declaration_name)),
optional(buildTypeConstraint($)),
),
)

export const _identifier_without_operators = () => IDENTIFIER
export const function_type = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.right(
seq(field('from', $._term), '=>', field('to', $._term)),
)

export const _operator = () => OPERATOR
export const optional_type = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => prec.left(20, seq(field('type', $._term), '?'))

export const map_type = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
seq(
'{',
'[',
optional(
seq(
field('property', alias($.type, $.type_variable_declaration_name)),
'in',
),
),
field('key', $._term),
']',
':',
field('value', $._term),
'}',
)

export const keyof_type = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
prec.right(seq('Keyof', field('type', $._term)))

export const type = () => TYPE

export const _identifier_without_operators = () => IDENTIFIER
export const _operator = () => OPERATOR
export const identifier = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => choice($._operator, $._identifier_without_operators)
Expand Down
Loading

0 comments on commit d065d3d

Please sign in to comment.