Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Commit

Permalink
arbos-before.mexe
Browse files Browse the repository at this point in the history
  • Loading branch information
rachel-bousfield committed Sep 9, 2021
1 parent 37ce168 commit 2300dac
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# Add your new mexe here
minitest_outputs = codeloadtest simple-closure closure generics/basic.mini generics/simple.mini generics/func.mini
minitest_outputs = codeloadtest simple-closure closure generics/basic generics/simple generics/func generics/closure
upgrade_outputs = regcopy_new regcopy_old upgrade1_new upgrade1_old
looptest_outputs = upgrade2_new upgrade2_old
builtin_outputs = arraytest globaltest kvstest maptest
Expand Down
2 changes: 1 addition & 1 deletion arb_os/arbos-upgrade.mexe

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion arb_os/arbos.mexe

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion looptest/upgrade2_base.mexe

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion looptest/upgrade2_new.mexe

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion looptest/upgrade2_old.mexe

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion minitests/generics/closure.mini
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ type storage<S> = struct {
};

func duplicate<U>(store: storage<U>, lambda: tupfunc<U>) -> (U, U) {
return lambda::<uint>(store.value);
return lambda(store.value);
}
35 changes: 30 additions & 5 deletions src/compile/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
//! Contains types and utilities for constructing the mini AST
use crate::compile::typecheck::{AbstractSyntaxTree, InliningMode, TypeCheckedNode};
use crate::compile::{path_display, CompileError, Lines};
use crate::compile::{path_display, CompileError, ErrorSystem, Lines};
use crate::console::Color;
use crate::link::{value_from_field_list, Import, TUPLE_SIZE};
use crate::mavm::{Instruction, LabelId, Value};
use crate::pos::{BytePos, Location};
use crate::stringtable::StringId;
use crate::stringtable::{StringId, StringTable};
use crate::uint256::Uint256;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeSet, HashMap, HashSet};
Expand Down Expand Up @@ -111,7 +111,7 @@ pub enum Type {
Every,
Option(Box<Type>),
Union(Vec<Type>),
Nominal(Vec<String>, StringId, Vec<Type>),
Nominal(Vec<String>, StringId, #[serde(default)] Vec<Type>),
Generic(usize),
ColoredAny(usize),
}
Expand Down Expand Up @@ -258,7 +258,10 @@ impl Type {
let mut tipe = self.clone();
tipe.replace(&mut |tipe| {
if let Type::Generic(slot) = tipe {
*tipe = specialization[*slot].clone();
match specialization.get(*slot) {
Some(specific) => *tipe = specific.clone(),
None => panic!("Generic func-call needs more generic args"),
}
}
});
tipe
Expand Down Expand Up @@ -961,7 +964,7 @@ impl Type {
Type::EthAddress => ("address".to_string(), type_set),
Type::Buffer => ("buffer".to_string(), type_set),
Type::Generic(id) => (format!("generic {}", id), type_set),
Type::ColoredAny(id) => (format!("colored_any {}", id), type_set),
Type::ColoredAny(id) => (format!("generic' {}", id), type_set),
Type::Tuple(subtypes) => {
let mut out = "(".to_string();
for s in subtypes {
Expand Down Expand Up @@ -1160,6 +1163,28 @@ impl Type {
}
}

/// Checks generic parameter names for duplicates.
pub fn check_generic_parameters(
params: Vec<(StringId, DebugInfo)>,
error_system: &mut ErrorSystem,
string_table: &StringTable,
) -> Vec<StringId> {
let mut seen = HashSet::new();
for (id, debug) in params.iter() {
if !seen.insert(id) {
error_system.errors.push(CompileError::new(
"Parser error",
format!(
"Duplicate generic parameter {}",
Color::red(string_table.name_from_id(*id))
),
debug.locs(),
));
}
}
params.into_iter().map(|(name, _)| name).collect()
}

pub fn type_vectors_covariant_castable(
tvec1: &[Type],
tvec2: &[Type],
Expand Down
38 changes: 22 additions & 16 deletions src/mini.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//


use crate::compile::ast::{TopLevelDecl, TypeDecl, Func, GlobalVar, Type, CodeBlock, StructField, FuncArg, FuncProperties, Statement, StatementKind, DebugInfo, Attributes, MatchPattern, SubData, Expr, ExprKind, TrinaryOp, BinaryOp, UnaryOp, Constant, OptionConst, FieldInitializer, new_func_arg, new_type_decl};
use crate::compile::ast::{TopLevelDecl, TypeDecl, Func, GlobalVar, Type, CodeBlock, StructField, FuncArg, FuncProperties, Statement, StatementKind, DebugInfo, Attributes, MatchPattern, SubData, Expr, ExprKind, TrinaryOp, BinaryOp, UnaryOp, Constant, OptionConst, FieldInitializer, new_func_arg, new_type_decl, check_generic_parameters};
use crate::compile::{InliningMode, ErrorSystem, CompileError};
use crate::stringtable::{StringTable, StringId};
use crate::compile::Lines;
Expand All @@ -16,7 +16,7 @@ use std::collections::{HashMap, HashSet, BTreeMap, BTreeSet};
use regex::Regex;

grammar(
stringtable: &mut StringTable, // map of ids to strings
string_table: &mut StringTable, // map of ids to strings
file_info: &Lines, // the lines of source code
filename: u64, // filename hash
current_path: &[String], // path to what's being parsed
Expand Down Expand Up @@ -60,7 +60,7 @@ pub Decls: Vec<TopLevelDecl> = {
HeadDecl: TopLevelDecl = {
<lno: @L> "use" <mut p: PathDecl> ";" => {
let file = p.pop().expect("Internal error: Path vector was empty");
let id = stringtable.get(file.clone());
let id = string_table.get(file.clone());
TopLevelDecl::UseDecl(Import::new(
p, file, Some(id), file_info.location(BytePos::from(lno), filename),
))
Expand Down Expand Up @@ -104,7 +104,10 @@ BodyDecl: TopLevelDecl = {

TypeDecl: TypeDecl = {
"type" <i: Ident> <g: Generalization?> "=" <t: Type> ";" => {
let names: Vec<_> = g.into_iter().flatten().collect();
let params = g.clone().into_iter().flatten().map(|(name, lno)| {
(name, DebugInfo::here(file_info, lno, filename))
}).collect();
let names = check_generic_parameters(params, error_system, &string_table);
new_type_decl(i, t.make_generic(&names))
}
}
Expand All @@ -115,9 +118,12 @@ FuncDecl: Func = {
let view = qualifiers.contains(&"view");
let write = qualifiers.contains(&"write");
let debug = DebugInfo::new(file_info.location(BytePos::from(lno), filename), attribs.unwrap_or_default());
let name = stringtable.name_from_id(i).clone();
let name = string_table.name_from_id(i).clone();

let names = g.into_iter().flatten().collect();
let params = g.into_iter().flatten().map(|(name, lno)| {
(name, DebugInfo::here(file_info, lno, filename))
}).collect();
let names = check_generic_parameters(params, error_system, &string_table);

args.iter_mut().for_each(|arg| arg.tipe = arg.tipe.make_generic(&names));
if let Some(ref mut tipe) = ret {
Expand Down Expand Up @@ -146,7 +152,7 @@ GlobalVarDecl: GlobalVar = {
let mut debug_info = DebugInfo::here(file_info, lno, filename);
debug_info.attributes = attribs.unwrap_or_default();
GlobalVar::new(
i, stringtable.name_from_id(i).clone(), t, debug_info
i, string_table.name_from_id(i).clone(), t, debug_info
)
},
}
Expand Down Expand Up @@ -250,7 +256,7 @@ StatementKind: StatementKind = {
}
SubData::Dot(id) => {
let inside = e.clone();
let name = stringtable.name_from_id(id).to_string();
let name = string_table.name_from_id(id).to_string();
*build = Expr {
kind: ExprKind::StructMod(Box::new(nest.clone()), name.clone(), Box::new(inside)),
debug_info,
Expand Down Expand Up @@ -291,7 +297,7 @@ StructFields: Vec<StructField> = {
}

StructField: StructField = {
<i: Ident> ":" <t: Type> => StructField::new(stringtable.name_from_id(i).to_string(), t),
<i: Ident> ":" <t: Type> => StructField::new(string_table.name_from_id(i).to_string(), t),
}

Type: Type = {
Expand Down Expand Up @@ -329,8 +335,8 @@ Type: Type = {
}
};

Generalization: Vec<StringId> = {
"<" <c: Comma<Ident>> ">" => c,
Generalization: Vec<(StringId, usize)> = {
"<" <c: Comma<(<Ident> <@R>)>> ">" => c,
}

Specialization: Vec<Type> = {
Expand All @@ -350,7 +356,7 @@ CommaedTypes: Vec<Type> = {
};

Ident: StringId = {
IdentString => stringtable.get(<>),
IdentString => string_table.get(<>),
};

Expr: Expr = {
Expand All @@ -363,7 +369,7 @@ Expr: Expr = {
debug_info: DebugInfo::from(file_info.location(BytePos::from(lno), filename))
},
<lno: @L> <t: Expr> "with" "{" <i:Ident> ":" <e: Expr> "}" => Expr {
kind: ExprKind::StructMod(Box::new(t), stringtable.name_from_id(i).to_string(), Box::new(e)),
kind: ExprKind::StructMod(Box::new(t), string_table.name_from_id(i).to_string(), Box::new(e)),
debug_info: DebugInfo::from(file_info.location(BytePos::from(lno), filename)),
},
Expr1,
Expand Down Expand Up @@ -451,7 +457,7 @@ Expr11: Expr = {
debug_info: DebugInfo::from(file_info.location(BytePos::from(lno), filename))
},
<lno: @L> <e:Expr11> "." <i:Ident> => Expr {
kind: ExprKind::DotRef(Box::new(e), stringtable.name_from_id(i).to_string()),
kind: ExprKind::DotRef(Box::new(e), string_table.name_from_id(i).to_string()),
debug_info: DebugInfo::from(file_info.location(BytePos::from(lno), filename))
},
<lno: @L> <e:Expr11> "." <u:UnsignedInteger> => Expr { kind: ExprKind::TupleRef(Box::new(e), u), debug_info: DebugInfo::from(file_info.location(BytePos::from(lno), filename))},
Expand Down Expand Up @@ -508,7 +514,7 @@ Expr12: Expr = {
let debug = DebugInfo::here(file_info, lno, filename);

let name = format!("closure #{}", closures.len() + 1);
let id = stringtable.get(name.clone());
let id = string_table.get(name.clone());
let closure = Func::new(name, id, false, view, write, true, fa, t, cb, BTreeSet::new(), vec![], 0, debug);

closures.insert(id, closure.clone());
Expand Down Expand Up @@ -546,7 +552,7 @@ FieldInitializers: Vec<FieldInitializer> = {
}

FieldInitializer: FieldInitializer = {
<i: Ident> ":" <e: Expr> => FieldInitializer::new(stringtable.name_from_id(i).to_string(), e),
<i: Ident> ":" <e: Expr> => FieldInitializer::new(string_table.name_from_id(i).to_string(), e),
}

CommaedExprs: Vec<Expr> = {
Expand Down
Loading

0 comments on commit 2300dac

Please sign in to comment.