Skip to content

Commit 1f64c82

Browse files
committed
better optimizations
1 parent 2accc2e commit 1f64c82

3 files changed

Lines changed: 22 additions & 28 deletions

File tree

crates/vim9-gen/src/lib.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ fn identifier_list(state: &mut State, unpacked: &UnpackIdentifier) -> String {
867867
.collect()
868868
}
869869

870-
fn guess_type_of_expr(_: &State, expr: &Expression) -> Type {
870+
fn guess_type_of_expr(state: &State, expr: &Expression) -> Type {
871871
match expr {
872872
Expression::Number(_) => Type::Number,
873873
Expression::String(_) => Type::String,
@@ -886,16 +886,21 @@ fn guess_type_of_expr(_: &State, expr: &Expression) -> Type {
886886
Expression::Infix(infix) => {
887887
if infix.operator.is_comparison() {
888888
return Type::Bool;
889-
} else if matches!(infix.operator, Operator::And | Operator::Or) {
890-
// if guess_type_of_expr(infix.left) {}
891-
Type::Any
892-
// todo!()
893-
} else {
894-
Type::Any
895889
}
890+
891+
if matches!(infix.operator, Operator::And | Operator::Or) {
892+
if guess_type_of_expr(state, infix.left.as_ref()) == Type::Bool
893+
&& guess_type_of_expr(state, infix.right.as_ref())
894+
== Type::Bool
895+
{
896+
return Type::Bool;
897+
}
898+
}
899+
900+
Type::Any
896901
}
902+
Expression::Grouped(g) => guess_type_of_expr(state, g.expr.as_ref()),
897903
_ => Type::Any,
898-
// Expression::Grouped(_) => todo!(),
899904
// Expression::Index(_) => todo!(),
900905
// Expression::Slice(_) => todo!(),
901906
// Expression::Array(_) => todo!(),

crates/vim9-gen/testdata/output/busted_multiline.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe("filename", function()
1111
vim.v.errors = {}
1212

1313
-- Actual test
14-
local x = NVIM9.convert.decl_bool(NVIM9.ops["And"](true, false))
14+
local x = NVIM9.convert.decl_bool(true and false)
1515

1616
NVIM9.fn["assert_equal"](x, false)
1717

@@ -24,7 +24,7 @@ describe("filename", function()
2424
vim.v.errors = {}
2525

2626
-- Actual test
27-
local y = NVIM9.convert.decl_bool(NVIM9.ops["And"](true, false))
27+
local y = NVIM9.convert.decl_bool(true and false)
2828

2929
NVIM9.fn["assert_equal"](y, false)
3030

@@ -37,7 +37,7 @@ describe("filename", function()
3737
vim.v.errors = {}
3838

3939
-- Actual test
40-
local z = NVIM9.ops["Or"](true, false)
40+
local z = true or false
4141

4242
NVIM9.fn["assert_equal"](z, true)
4343

crates/vim9-gen/testdata/output/vim9_gen__test__cfilter.snap

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: crates/vim9-gen/src/lib.rs
3-
assertion_line: 1537
3+
assertion_line: 1542
44
expression: "generate(contents, false).unwrap()"
55
---
66
local NVIM9 = require("vim9script")
@@ -82,14 +82,7 @@ Qf_filter = function(qf, searchpat, bang)
8282

8383
firstchar = NVIM9.index(searchpat, 0)
8484
lastchar = NVIM9.slice(searchpat, -1, nil)
85-
if
86-
NVIM9.bool(
87-
NVIM9.ops["And"](
88-
firstchar == lastchar,
89-
(NVIM9.ops["Or"](NVIM9.ops["Or"](firstchar == "/", firstchar == '"'), firstchar == "'"))
90-
)
91-
)
92-
then
85+
if NVIM9.bool(firstchar == lastchar and (firstchar == "/" or firstchar == '"' or firstchar == "'")) then
9386
pat = NVIM9.slice(searchpat, 1, -2)
9487
if NVIM9.bool(pat == "") then
9588
-- # Use the last search pattern
@@ -105,17 +98,13 @@ Qf_filter = function(qf, searchpat, bang)
10598

10699
if NVIM9.bool(bang == "!") then
107100
Cond = function(_, val)
108-
return NVIM9.ops["And"](
109-
NVIM9.ops["NotRegexpMatches"](val["text"], pat),
110-
NVIM9.ops["NotRegexpMatches"](NVIM9.fn["bufname"](val["bufnr"]), pat)
111-
)
101+
return NVIM9.ops["NotRegexpMatches"](val["text"], pat)
102+
and NVIM9.ops["NotRegexpMatches"](NVIM9.fn["bufname"](val["bufnr"]), pat)
112103
end
113104
else
114105
Cond = function(_, val)
115-
return NVIM9.ops["Or"](
116-
NVIM9.ops["RegexpMatches"](val["text"], pat),
117-
NVIM9.ops["RegexpMatches"](NVIM9.fn["bufname"](val["bufnr"]), pat)
118-
)
106+
return NVIM9.ops["RegexpMatches"](val["text"], pat)
107+
or NVIM9.ops["RegexpMatches"](NVIM9.fn["bufname"](val["bufnr"]), pat)
119108
end
120109
end
121110

0 commit comments

Comments
 (0)