2020-10-07 03:20:51 +03:00
|
|
|
// Copyright (C) 2019-2020 Aleo Systems Inc.
|
|
|
|
// This file is part of the Leo library.
|
|
|
|
|
|
|
|
// The Leo library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// The Leo library 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
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
2020-10-27 05:13:37 +03:00
|
|
|
|
|
|
|
pub mod arrays;
|
2020-10-27 06:02:44 +03:00
|
|
|
pub mod circuits;
|
|
|
|
pub mod functions;
|
2020-10-27 05:13:37 +03:00
|
|
|
pub mod tuples;
|
2020-10-27 04:41:51 +03:00
|
|
|
pub mod variables;
|
2020-10-07 03:20:51 +03:00
|
|
|
|
2020-10-31 03:17:17 +03:00
|
|
|
use leo_grammar::Grammar;
|
2020-10-30 22:49:01 +03:00
|
|
|
use leo_type_inference::TypeInference;
|
2020-10-07 03:20:51 +03:00
|
|
|
|
2020-10-31 03:31:09 +03:00
|
|
|
use leo_ast::{Input, LeoAst, Program};
|
2020-10-27 02:46:11 +03:00
|
|
|
use leo_imports::ImportParser;
|
2020-10-30 20:51:45 +03:00
|
|
|
use leo_symbol_table::SymbolTable;
|
2020-10-07 03:20:51 +03:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
const TEST_PROGRAM_PATH: &str = "";
|
|
|
|
const TEST_PROGRAM_NAME: &str = "test";
|
|
|
|
|
2020-10-30 22:39:02 +03:00
|
|
|
/// A helper struct to test a `TypeInference` check.
|
|
|
|
pub struct TestTypeInference {
|
2020-10-27 04:41:51 +03:00
|
|
|
program: Program,
|
|
|
|
symbol_table: SymbolTable,
|
2020-10-07 03:20:51 +03:00
|
|
|
}
|
|
|
|
|
2020-10-30 22:39:02 +03:00
|
|
|
impl TestTypeInference {
|
2020-10-07 03:20:51 +03:00
|
|
|
pub fn new(bytes: &[u8]) -> Self {
|
|
|
|
// Get file string from bytes.
|
|
|
|
let file_string = String::from_utf8_lossy(bytes);
|
|
|
|
|
|
|
|
// Get test file path.
|
|
|
|
let file_path = PathBuf::from(TEST_PROGRAM_PATH);
|
|
|
|
|
|
|
|
// Get parser syntax tree.
|
2020-10-31 03:17:17 +03:00
|
|
|
let ast = Grammar::new(&file_path, &*file_string).unwrap();
|
2020-10-07 03:20:51 +03:00
|
|
|
|
|
|
|
// Get typed syntax tree.
|
2020-10-31 03:31:09 +03:00
|
|
|
let typed = LeoAst::new(TEST_PROGRAM_NAME, &ast);
|
2020-10-07 03:20:51 +03:00
|
|
|
let program = typed.into_repr();
|
|
|
|
|
2020-10-27 02:46:11 +03:00
|
|
|
// Create empty import parser.
|
2020-11-11 03:23:55 +03:00
|
|
|
let import_parser = ImportParser::default();
|
2020-10-27 02:46:11 +03:00
|
|
|
|
|
|
|
// Create empty input.
|
|
|
|
let input = Input::new();
|
|
|
|
|
2020-10-30 20:44:44 +03:00
|
|
|
// Create symbol table.
|
|
|
|
let symbol_table = SymbolTable::new(&program, &import_parser, &input).unwrap();
|
2020-10-07 03:20:51 +03:00
|
|
|
|
2020-10-30 22:39:02 +03:00
|
|
|
// Store fields for new type inference check.
|
2020-10-27 04:41:51 +03:00
|
|
|
Self { program, symbol_table }
|
2020-10-07 03:20:51 +03:00
|
|
|
}
|
|
|
|
|
2020-10-30 22:39:02 +03:00
|
|
|
pub fn check(self) {
|
2020-11-11 03:23:55 +03:00
|
|
|
TypeInference::run(&self.program, self.symbol_table).unwrap();
|
2020-10-27 04:41:51 +03:00
|
|
|
}
|
|
|
|
|
2020-10-27 09:42:01 +03:00
|
|
|
pub fn expect_error(self) {
|
2020-11-11 03:23:55 +03:00
|
|
|
assert!(TypeInference::run(&self.program, self.symbol_table).is_err());
|
2020-10-27 04:41:51 +03:00
|
|
|
}
|
2020-10-07 03:20:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new() {
|
|
|
|
let bytes = include_bytes!("empty.leo");
|
|
|
|
|
2020-10-30 22:39:02 +03:00
|
|
|
let type_inference = TestTypeInference::new(bytes);
|
2020-10-07 03:20:51 +03:00
|
|
|
|
2020-10-30 22:39:02 +03:00
|
|
|
type_inference.check()
|
2020-10-07 03:20:51 +03:00
|
|
|
}
|