Skip to content

Commit

Permalink
Merge pull request #156 from JSAbrahams/type-checker
Browse files Browse the repository at this point in the history
First version of the type checker
  • Loading branch information
JSAbrahams authored Nov 20, 2019
2 parents f901d9f + c861aa1 commit 1ae4d16
Show file tree
Hide file tree
Showing 217 changed files with 8,935 additions and 3,391 deletions.
8 changes: 3 additions & 5 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ branches:

environment:
matrix:
# Stable 64-bit MSVC
- channel: stable
PYTHON: "C:\\Python37"
target: x86_64-pc-windows-msvc
# Nightly 64-bit MSVC
- channel: nightly
PYTHON: "C:\\Python37"
Expand All @@ -27,8 +23,9 @@ install:

- if %channel% == "nightly" rustup install nightly

- if %channel% == "nightly" rustup component add rustfmt --toolchain nightly
- if %channel% == "nightly" rustup component add rustfmt --toolchain nightly
- if %channel% == "stable" rustup component add clippy
- if %channel% == "nightly" rustup component add clippy --toolchain nightly

- rustc -vV
- cargo -vV
Expand All @@ -38,6 +35,7 @@ build: false
test_script:
- if %channel% == "nightly" cargo +nightly fmt --all -- --check
- if %channel% == "stable" cargo clippy -- -D warnings
- if %channel% == "nightly" cargo +nightly clippy -- -D warnings

- cargo test --verbose

Expand Down
2 changes: 1 addition & 1 deletion .codacy.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
exclude_paths:
- 'tests/resources/**'
- '**/*.py'
22 changes: 15 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ os:

language: rust
rust:
- stable
- nightly

dist: xenial
python:
- "3.7"
- "3.8"

# cache dependencies
before_cache:
Expand All @@ -20,8 +19,6 @@ cache:
- if [ $TRAVIS_OS_NAME = 'osx' ]; then $HOME/Library/Caches/Homebrew; fi

sudo: true
before_install:
- sudo apt-get update

addons:
apt:
Expand All @@ -38,14 +35,24 @@ branches:
- master

before_script:
# Python
- if [ $TRAVIS_OS_NAME = 'linux' ]; then sudo apt-get install python3; fi

# Cargo components
- if [ $TRAVIS_RUST_VERSION = 'nightly' AND $TRAVIS_OS_NAME = 'linux' ]; then rustup component add rustfmt --toolchain nightly; fi
- if [ $TRAVIS_RUST_VERSION = 'stable' AND $TRAVIS_OS_NAME = 'linux' ]; then rustup component add clippy; fi
- if [ $TRAVIS_RUST_VERSION = 'nightly' AND $TRAVIS_OS_NAME = 'linux' ]; then rustup component add clippy --toolchain nightly; fi

before_install:
# Python
- python3 --version
- |
if [ $TRAVIS_OS_NAME = 'linux' ]; then
sudo apt update;
sudo apt install software-properties-common;
sudo add-apt-repository -y ppa:deadsnakes/ppa;
sudo apt-get update;
sudo apt-get install python3.8;
fi
- python3 --version

# grcov
- curl -L https://github.com/mozilla/grcov/releases/download/v0.4.1/grcov-linux-x86_64.tar.bz2 | tar jxf -

Expand All @@ -55,6 +62,7 @@ after_success:

script:
- if [ $TRAVIS_RUST_VERSION = 'stable' AND $TRAVIS_OS_NAME = 'linux' ]; then cargo clippy -- -D warnings; fi
- if [ $TRAVIS_RUST_VERSION = 'nightly' AND $TRAVIS_OS_NAME = 'linux' ]; then cargo +nightly clippy -- -D warnings; fi
- if [ $TRAVIS_RUST_VERSION = 'nightly' AND $TRAVIS_OS_NAME = 'linux' ]; then cargo +nightly fmt --all -- --check; fi

- cargo build
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
[package]
name = "mamba"
version = "0.1.0"
version = "0.2.0"
authors = ["Joel Abrahams <[email protected]>"]
description = "A transpiler which translates Mamba to Python 3 files"
edition = "2018"
license = "MIT"
license-file = "LICENSE"

readme = "README.md"
Expand Down
1 change: 1 addition & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod position;
72 changes: 72 additions & 0 deletions src/common/position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use std::cmp::{max, min};

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
/// A position represents a rectangle in the source code.
pub struct Position {
pub start: CaretPos,
pub end: CaretPos
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
/// An endpoint represents either the top left or bottom right points of a
/// [Position] rectangle.
///
/// Line's and position's are 1-indexed.
pub struct CaretPos {
pub line: i32,
pub pos: i32
}

impl Position {
pub fn new(start: &CaretPos, end: &CaretPos) -> Position {
Position { start: start.clone(), end: end.clone() }
}

/// Get the absolute width of a position, which represents a rectangle in
/// the source code.
///
/// Width is always 1 or greater.
pub fn get_width(&self) -> i32 {
max(1, max(self.end.pos - self.start.pos, self.start.pos - self.end.pos))
}

pub fn union(&self, other: &Position) -> Position {
Position {
start: CaretPos {
line: min(self.start.line, other.start.line),
pos: min(self.start.pos, other.start.pos)
},
end: CaretPos {
line: max(self.end.line, other.end.line),
pos: max(self.end.pos, other.end.pos)
}
}
}
}

impl CaretPos {
/// Create new endpoint with given line and position.
pub fn new(line: i32, pos: i32) -> CaretPos { CaretPos { line, pos } }

/// Create new [EndPoint] which is offset in the vertical direction by the
/// given amount.
pub fn offset_line(self, offset: i32) -> CaretPos {
CaretPos { line: self.line + offset, pos: self.pos }
}

/// Create new [EndPoint] which is offset in the horizontal direction by the
/// given amount.
pub fn offset_pos(self, offset: i32) -> CaretPos {
CaretPos { line: self.line, pos: self.pos + offset }
}

pub fn newline(self) -> CaretPos { CaretPos { line: self.line + 1, pos: 1 } }
}

impl Default for Position {
fn default() -> Self { Position { start: CaretPos::default(), end: CaretPos::default() } }
}

impl Default for CaretPos {
fn default() -> Self { CaretPos { line: 1, pos: 1 } }
}
Loading

0 comments on commit 1ae4d16

Please sign in to comment.