Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed metavar (part 2, sgrep part) for literals. #88

Merged
merged 3 commits into from Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -22,7 +22,7 @@ dist/
downloads/
eggs/
.eggs/
lib/
#lib/ pad: used for ocaml
lib64/
parts/
sdist/
Expand Down
7 changes: 7 additions & 0 deletions lib/generic_vs_generic.ml
Expand Up @@ -449,6 +449,12 @@ and m_expr a b =
when MV.is_metavar_name str ->
envf (str, tok) (B.E (e2))

(* metavar: typed! *)
| A.TypedMetavar ((str, tok), _, t), e2
when MV.is_metavar_name str &&
Typechecking_generic.compatible_type t e2 ->
envf (str, tok) (B.E e2)

(* dots: should be patterned-match before in arguments, or statements,
* but this is useful for keyword parameters, as in f(..., foo=..., ...)
*)
Expand Down Expand Up @@ -593,6 +599,7 @@ and m_expr a b =
| A.Yield _, _ | A.Await _, _ | A.Cast _, _ | A.Seq _, _ | A.Ref _, _
| A.DeRef _, _ | A.OtherExpr _, _
| A.SliceAccess _, _
| A.TypedMetavar _, _
-> fail ()


Expand Down
45 changes: 45 additions & 0 deletions lib/typechecking_generic.ml
@@ -0,0 +1,45 @@
(* Yoann Padioleau
*
* Copyright (C) 2020 r2c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License (GPL)
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* file license.txt for more details.
*)
open Ast_generic

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* Poor's man typechecker on literals (for now).
*
* todo:
* - local type inference on AST generic? good coverage?
* - we could allow metavars on the type itself, as in
* foo($X: $T) ... $T x; ...
* which would require to transform the code in the generic_vs_generic
* style as typechecking could also bind metavariables in the process
*)

(*****************************************************************************)
(* Entry point *)
(*****************************************************************************)

(* very Python specific for now where the type is currently an OT_Expr
* TODO: fill Ast_generic.expr_to_type at least.
*)
let compatible_type t e =
match t, e with
| OtherType (OT_Expr, [E (Name ((("int", _tok), _nameinfo), _idinfo))]),
L (Int _) -> true
| OtherType (OT_Expr, [E (Name ((("float", _tok), _nameinfo), _idinfo))]),
L (Float _) -> true
| OtherType (OT_Expr, [E (Name ((("str", _tok), _nameinfo), _idinfo))]),
L (String _) -> true

| _ -> false
2 changes: 2 additions & 0 deletions lib/typechecking_generic.mli
@@ -0,0 +1,2 @@

val compatible_type: Ast_generic.type_ -> Ast_generic.expr -> bool
5 changes: 5 additions & 0 deletions tests/python/metavar_typed.py
@@ -0,0 +1,5 @@
def foo():
#ERROR: match
foo(1)
foo(1.0)
foo("")
1 change: 1 addition & 0 deletions tests/python/metavar_typed.sgrep
@@ -0,0 +1 @@
foo($X: int)