This commit is contained in:
Michael Zhang 2022-06-17 09:50:06 -05:00
parent e86271b190
commit b95a39b1e8
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
8 changed files with 80 additions and 15 deletions

View file

@ -1,15 +1,16 @@
{ rustPlatform, lib, nix-gitignore, llvmPackages_12 }:
{ rustPlatform, lib, nix-gitignore, llvmPkgs, libffi }:
let llvmPkgs = llvmPackages_12;
in with llvmPkgs;
with llvmPkgs;
rustPlatform.buildRustPackage {
name = "e0";
src = nix-gitignore.gitignoreSource [ ./.gitignore ] ./.;
buildInputs = [ llvmPkgs.llvm.dev ];
nativeBuildInputs = [ llvm.dev ];
buildInputs = [ libffi ];
cargoSha256 = lib.fakeSha256;
# cargoSha256 = "sha256-rWMnlkb/XoycP69NOOmg7Wd4qPgliuvOKGWaEth/8tg=";
LLVM_SYS_120_PREFIX = "${llvm.dev}";
# cargoSha256 = lib.fakeSha256;
cargoSha256 = "sha256-gPaYoHcNltAe/lK3pLhLZnX3Q8Pxj6yB45L/XWn3ZKc=";
}

13
examples/struct.e0 Normal file
View file

@ -0,0 +1,13 @@
type IntPair = struct {
fst: int,
snd: int,
}
fn main() -> int {
let x = IntPair {
fst: 4,
snd: 6,
};
return x.fst + x.snd;
}

View file

@ -2,11 +2,11 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1648219316,
"narHash": "sha256-Ctij+dOi0ZZIfX5eMhgwugfvB+WZSrvVNAyAuANOsnQ=",
"lastModified": 1652252629,
"narHash": "sha256-SvT64apetqc8P5nYp1/fOZvUmHUPdPFUZbhSpKy+1aI=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "30d3d79b7d3607d56546dd2a6b49e156ba0ec634",
"rev": "d2fc6856824cb87742177eefc8dd534bdb6c3439",
"type": "github"
},
"original": {
@ -22,11 +22,11 @@
},
"utils": {
"locked": {
"lastModified": 1648297722,
"narHash": "sha256-W+qlPsiZd8F3XkzXOzAoR+mpFqzm3ekQkJNa+PIh1BQ=",
"lastModified": 1649676176,
"narHash": "sha256-OWKJratjt2RW151VUlJPRALb7OU2S5s+f0vLj4o1bHM=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "0f8662f1319ad6abf89b3380dd2722369fc51ade",
"rev": "a4b154ebbdc88c8498a5c7b01589addc9e9cb678",
"type": "github"
},
"original": {

View file

@ -7,9 +7,15 @@
utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
myPkgs = rec { e0 = pkgs.callPackage ./. { }; };
llvmPkgs = pkgs.llvmPackages_12;
myPkgs = rec { e0 = pkgs.callPackage ./. { inherit llvmPkgs; }; };
in rec {
devShell = pkgs.mkShell {
packages = with pkgs; [ llvmPkgs.clangUseLLVM cargo-watch ];
inputsFrom = [ myPkgs.e0 ];
};
packages = utils.lib.flattenTree myPkgs;
defaultPackage = packages.e0;
});

25
scripts/e0c Executable file
View file

@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -e
SOURCE=$?
BITCODE=$(mktemp -d)
OUTPUT=./a.out
usage() { echo "$0 options:" && grep " .)\ #" $0; exit 0; }
[ $# -eq 0 ] && usage
while getopts ":hs:p:" arg; do
case $arg in
o) # Binary output
OUTPUT=${OPTARG}
;;
h | *) # Display help.
usage
exit 0
;;
esac
done
cargo run -- -o $BITCODE $SOURCE
clang -o $OUTPUT $BITCODE

View file

@ -75,5 +75,6 @@ pub enum Type {
Int,
Bool,
StructInst(Vec<(String, Type)>),
Func(Vec<Type>, Box<Type>),
}

View file

@ -16,6 +16,7 @@ pub enum Type_ {
Int,
Bool,
StructInst(Vec<(String, Type_)>),
Func(Vec<Type_>, Box<Type_>),
}
@ -30,6 +31,12 @@ impl Type_ {
let ret = Type_::from_type(*ret);
Type_::Func(args, Box::new(ret))
}
Type::StructInst(fields) => Type_::StructInst(
fields
.into_iter()
.map(|(name, ty)| (name, Type_::from_type(ty)))
.collect(),
),
}
}
@ -51,6 +58,12 @@ impl Type_ {
let ret = ret.convert(assignments)?;
Type::Func(args, Box::new(ret))
}
Type_::StructInst(fields) => Type::StructInst(
fields
.into_iter()
.map(|(name, ty)| Ok((name.clone(), ty.convert(assignments)?)))
.collect::<Result<_>>()?,
),
})
}
}
@ -325,6 +338,12 @@ fn substitute_types(assignments: &Assignments, ty: &Type_) -> Type_ {
let ret = substitute_types(assignments, &*ret);
Type_::Func(args, Box::new(ret))
}
Type_::StructInst(fields) => Type_::StructInst(
fields
.into_iter()
.map(|(name, ty)| (name.clone(), substitute_types(assignments, ty)))
.collect(),
),
Type_::Int | Type_::Bool => ty.clone(),
}
}
@ -501,7 +520,6 @@ pub fn convert(ast: Vec<Decl<()>>) -> Result<Vec<Decl<Type>>> {
collect_info(scoped_env, func)?;
env = *env2.parent.unwrap();
let assignments = unify_constraints(&constraints)?;
let typed_func = substitute_in_func(&assignments, decorated_func)?;

1
tokei.toml Normal file
View file

@ -0,0 +1 @@
[[languages]]