Skip to content

Commit

Permalink
Allow where clause for get.
Browse files Browse the repository at this point in the history
Resolves #49
  • Loading branch information
gdotdesign committed Jul 27, 2018
1 parent 16972d5 commit c5d3118
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 3 deletions.
35 changes: 35 additions & 0 deletions spec/compilers/get_with_where
@@ -0,0 +1,35 @@
store A {
get test : String {
something
} where {
something =
"Asd"

otherThing =
0
}
}
--------------------------------------------------------------------------------
const $A = new(class extends Store {
constructor() {
super()
this.props = {

}
}

get state() {
return {

}
}

get test() {
let something = `Asd`

let otherThing = 0

return something
}
})
$A.__displayName = `A`
12 changes: 12 additions & 0 deletions spec/formatters/get_with_where
@@ -0,0 +1,12 @@
store A {
gettest:Void{b}where{b=void}
}
--------------------------------------------------------------------------------
store A {
get test : Void {
b
} where {
b =
void
}
}
17 changes: 17 additions & 0 deletions spec/type_checking/get_returning_where
@@ -0,0 +1,17 @@
store A {
get test : Bool {
x
} where {
x =
true
}
}
-----------------------------------------------------------------GetTypeMismatch
store A {
get test : Bool {
x
} where {
x =
"hello"
}
}
3 changes: 2 additions & 1 deletion src/ast/get.cr
@@ -1,13 +1,14 @@
module Mint
class Ast
class Get < Node
getter name, body, type, comment, head_comments, tail_comments
getter name, body, type, comment, head_comments, tail_comments, where

def initialize(@head_comments : Array(Comment),
@tail_comments : Array(Comment),
@comment : Comment?,
@body : Expression,
@name : Variable,
@where : Where?,
@input : Data,
@from : Int32,
@type : Type,
Expand Down
12 changes: 11 additions & 1 deletion src/compilers/get.cr
Expand Up @@ -4,11 +4,21 @@ module Mint
body =
compile node.body

wheres =
compile node.where.try(&.statements) || [] of Ast::WhereStatement

wheres_separator =
wheres.any? ? "\n\n" : ""

name =
node.name.value

body =
"return #{body}".indent
[wheres.join("\n\n"),
wheres_separator,
"return #{body}",
].join("")
.indent

"get #{name}() {\n#{body}\n}"
end
Expand Down
5 changes: 4 additions & 1 deletion src/formatters/get.cr
Expand Up @@ -10,10 +10,13 @@ module Mint
body =
list [node.body] + node.head_comments + node.tail_comments

where =
format node.where

comment =
node.comment.try { |item| "#{format(item)}\n" }

"#{comment}get #{name} : #{type} {\n#{body.indent}\n}"
"#{comment}get #{name} : #{type} {\n#{body.indent}\n}#{where}"
end
end
end
3 changes: 3 additions & 0 deletions src/parsers/get.cr
Expand Up @@ -29,12 +29,15 @@ module Mint
expression! GetExpectedExpression
end

whitespace

Ast::Get.new(
head_comments: head_comments,
tail_comments: tail_comments,
from: start_position,
comment: comment,
to: position,
where: where,
input: data,
name: name,
body: body,
Expand Down
2 changes: 2 additions & 0 deletions src/type_checkers/get.cr
Expand Up @@ -10,6 +10,8 @@ module Mint
return_type =
resolve node.type

node.where.try { |item| resolve item }

raise GetTypeMismatch, {
"expected" => return_type,
"got" => body_type,
Expand Down
4 changes: 4 additions & 0 deletions src/type_checkers/scope.cr
Expand Up @@ -101,6 +101,10 @@ module Mint
node.where.try(&.statements.find(&.name.value.==(variable)))
end

def find(variable : String, node : Ast::Get)
node.where.try(&.statements.find(&.name.value.==(variable)))
end

def find(variable : String, node : Ast::InlineFunction)
node.arguments.find(&.name.value.==(variable))
end
Expand Down

0 comments on commit c5d3118

Please sign in to comment.