checker: prepare for the is_changed check

pull/5165/head
Alexander Medvednikov 2020-06-02 09:00:51 +02:00
parent 9fcb8536e8
commit 138d730dfa
4 changed files with 35 additions and 12 deletions

View File

@ -362,7 +362,7 @@ fn (mut g Game) draw_ui() {
if g.state == .gameover { if g.state == .gameover {
g.gg.draw_rect(0, win_height / 2 - text_size, win_width, g.gg.draw_rect(0, win_height / 2 - text_size, win_width,
5 * text_size, ui_color) 5 * text_size, ui_color)
g.ft.draw_text(1, win_height / 2 + 0 * text_size, 'Game Over', over_cfg) g.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Over', over_cfg)
g.ft.draw_text(1, win_height / 2 + 2 * text_size, 'Space to restart', over_cfg) g.ft.draw_text(1, win_height / 2 + 2 * text_size, 'Space to restart', over_cfg)
} else if g.state == .paused { } else if g.state == .paused {
g.gg.draw_rect(0, win_height / 2 - text_size, win_width, g.gg.draw_rect(0, win_height / 2 - text_size, win_width,

View File

@ -295,6 +295,7 @@ pub mut:
typ table.Type typ table.Type
pos token.Position pos token.Position
is_used bool is_used bool
is_changed bool // to detect mutable vars that are never changed
} }
pub struct GlobalDecl { pub struct GlobalDecl {

View File

@ -59,8 +59,23 @@ pub fn (mut c Checker) check(ast_file ast.File) {
for stmt in ast_file.stmts { for stmt in ast_file.stmts {
c.stmt(stmt) c.stmt(stmt)
} }
// Check scopes
// TODO
/*
for i, obj in c.file.global_scope.objects {
match obj {
ast.Var {
if it.is_mut && !it.is_changed {
c.warn('`$it.name` is declared as mutable, but it was never changed', it.pos)
}
}
else {}
}
}
*/
} }
// not used right now
pub fn (mut c Checker) check2(ast_file ast.File) []errors.Error { pub fn (mut c Checker) check2(ast_file ast.File) []errors.Error {
c.file = ast_file c.file = ast_file
for stmt in ast_file.stmts { for stmt in ast_file.stmts {
@ -583,6 +598,7 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) {
c.error('`$it.name` is immutable, declare it with `mut` to make it mutable', c.error('`$it.name` is immutable, declare it with `mut` to make it mutable',
it.pos) it.pos)
} }
v.is_changed = true
} else if it.name in c.const_names { } else if it.name in c.const_names {
c.error('cannot modify constant `$it.name`', it.pos) c.error('cannot modify constant `$it.name`', it.pos)
} }

View File

@ -12,9 +12,9 @@ import v.util
import v.errors import v.errors
import os import os
import runtime import runtime
//import sync
import time import time
// import sync
pub struct Parser { pub struct Parser {
file_name string // "/home/user/hello.v" file_name string // "/home/user/hello.v"
file_name_dir string // "/home/user" file_name_dir string // "/home/user"
@ -173,7 +173,6 @@ fn (mut q Queue) run() {
} }
} }
*/ */
pub fn parse_files(paths []string, table &table.Table, pref &pref.Preferences, global_scope &ast.Scope) []ast.File { pub fn parse_files(paths []string, table &table.Table, pref &pref.Preferences, global_scope &ast.Scope) []ast.File {
// println('nr_cpus= $nr_cpus') // println('nr_cpus= $nr_cpus')
$if macos { $if macos {
@ -234,6 +233,7 @@ pub fn (mut p Parser) open_scope() {
} }
pub fn (mut p Parser) close_scope() { pub fn (mut p Parser) close_scope() {
// TODO move this to checker since is_changed is set there?
if !p.pref.is_repl && !p.scanner.is_fmt { if !p.pref.is_repl && !p.scanner.is_fmt {
for _, obj in p.scope.objects { for _, obj in p.scope.objects {
match obj { match obj {
@ -245,6 +245,12 @@ pub fn (mut p Parser) close_scope() {
p.warn_with_pos('unused variable: `$it.name`', it.pos) p.warn_with_pos('unused variable: `$it.name`', it.pos)
} }
} }
/*
if it.is_mut && !it.is_changed {
p.warn_with_pos('`$it.name` is declared as mutable, but it was never changed',
it.pos)
}
*/
} }
else {} else {}
} }
@ -1486,7 +1492,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
} else { } else {
table.Language.v table.Language.v
} }
p.table.register_type_symbol(table.TypeSymbol{ p.table.register_type_symbol({
kind: .alias kind: .alias
name: p.prepend_mod(name) name: p.prepend_mod(name)
parent_idx: pid parent_idx: pid