darklang / dark

Darklang main repo, including language, backend, and infra
https://darklang.com
Other
1.68k stars 91 forks source link

Tree-sitter grammar: support the remaining infix operators #5322

Closed OceanOak closed 7 months ago

OceanOak commented 7 months ago

Changelog:

Tree-sitter-darklang
- Update the grammar to support the remaining infix operators, and update the parser accordingly

5321

OceanOak commented 7 months ago

Questions:

  1. Is it worth adding something like the following to writtenTypesToProgramTypes.dark, or is the approach implemented in this PR acceptable?
      module InfixFnName =
        let toPT (infix: WrittenTypes.InfixOperator) : ProgramTypes.InfixFnName =
          match infix with
          | ArithmeticPlus -> ProgramTypes.InfixFnName.ArithmeticPlus
          | ArithmeticMinus -> ProgramTypes.InfixFnName.ArithmeticMinus
          | ArithmeticMultiply -> ProgramTypes.InfixFnName.ArithmeticMultiply
          | ArithmeticDivide -> ProgramTypes.InfixFnName.ArithmeticDivide
          | ArithmeticModulo -> ProgramTypes.InfixFnName.ArithmeticModulo
          | ArithmeticPower -> ProgramTypes.InfixFnName.ArithmeticPower
          | ComparisonGreaterThan -> ProgramTypes.InfixFnName.ComparisonGreaterThan
          | ComparisonGreaterThanOrEqual ->
            ProgramTypes.InfixFnName.ComparisonGreaterThanOrEqual
          | ComparisonLessThan -> ProgramTypes.InfixFnName.ComparisonLessThan
          | ComparisonLessThanOrEqual ->
            ProgramTypes.InfixFnName.ComparisonLessThanOrEqual
          | ComparisonEqual -> ProgramTypes.InfixFnName.ComparisonEquals
          | ComparisonNotEqual -> ProgramTypes.InfixFnName.ComparisonNotEquals
          | StringConcat -> ProgramTypes.InfixFnName.StringConcat

      module BinaryOperation =
        let toPT
          (binop: WrittenTypes.BinaryOperator)
          : ProgramTypes.BinaryOperation =
          match binop with
          | BinOpAnd -> ProgramTypes.BinaryOperation.BinOpAnd
          | BinOpOr -> ProgramTypes.BinaryOperation.BinOpOr
  1. In tests/corpus, should we include tests similar to the following, or should we limit it to testing aspects like precedence?
    
    ==================
    comparison operator ==
    ==================

a == b


(source_file (expression (infix_operation (expression (variable_identifier)) (operator) (expression (variable_identifier)) ) ) )

================== exponent operator ^

a ^ b


(source_file (expression (infix_operation (expression (variable_identifier)) (operator) (expression (variable_identifier)) ) ) )

StachuDotNet commented 7 months ago

Questions:

  1. Is it worth adding something like the following to writtenTypesToProgramTypes.dark, or is the approach implemented in this PR acceptable?

I think this it'd be worth refactoring that fn out, yeah

  1. In tests/corpus, should we include tests similar to the following, or should we limit it to testing aspects like precedence?

Hm maybe I'm missing why you'd question these tests -- they seem useful to me. We don't have that many operators, so a handful of tests for each seems OK. The tree-sitter corpus tests run super fast, anyway.