Skip to content

Commit

Permalink
Interpreter.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebenfield committed Nov 12, 2024
1 parent 1e31a90 commit 9e1108e
Show file tree
Hide file tree
Showing 12 changed files with 3,255 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ members = [
"compiler/span",
"docs/grammar",
"errors",
"interpreter",
"leo/package",
"tests/test-framework",
"utils/disassembler",
Expand All @@ -60,6 +61,10 @@ version = "2.3.0"
path = "./errors"
version = "2.3.0"

[workspace.dependencies.leo-interpreter]
path = "./interpreter"
version = "2.3.0"

[workspace.dependencies.leo-package]
path = "./leo/package"
version = "2.3.0"
Expand All @@ -84,6 +89,9 @@ version = "2.3.0"
version = "0.1.24"
default-features = false

[workspace.dependencies.colored]
version = "2.0"

[workspace.dependencies.indexmap]
version = "2.6"
features = [ "serde" ]
Expand All @@ -99,6 +107,7 @@ default-features = false
version = "1.11.1"

[workspace.dependencies.snarkvm]
# path = "../SnarkVM"
version = "1.0.0"

[workspace.dependencies.serde]
Expand Down Expand Up @@ -145,6 +154,9 @@ workspace = true
[dependencies.leo-errors]
workspace = true

[dependencies.leo-interpreter]
workspace = true

[dependencies.leo-package]
workspace = true

Expand All @@ -165,6 +177,7 @@ version = "4.5"
features = [ "derive", "env", "color", "unstable-styles" ]

[dependencies.colored]
workspace = true
version = "2.0"

[dependencies.dotenvy]
Expand Down
24 changes: 23 additions & 1 deletion compiler/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use crate::{Token, tokenizer::*};

use leo_ast::*;
use leo_errors::{Result, emitter::Handler};
use leo_errors::{ParserError, Result, emitter::Handler};
use leo_span::{Span, span::BytePos};

use snarkvm::prelude::Network;
Expand All @@ -49,3 +49,25 @@ pub fn parse<N: Network>(

tokens.parse_program()
}

pub fn parse_expression<N: Network>(handler: &Handler, node_builder: &NodeBuilder, source: &str) -> Result<Expression> {
let mut context = ParserContext::<N>::new(handler, node_builder, crate::tokenize(source, BytePos(0))?);

let expression = context.parse_expression()?;
if context.token.token == Token::Eof {
Ok(expression)
} else {
Err(ParserError::unexpected(context.token.token, Token::Eof, context.token.span).into())
}
}

pub fn parse_statement<N: Network>(handler: &Handler, node_builder: &NodeBuilder, source: &str) -> Result<Statement> {
let mut context = ParserContext::<N>::new(handler, node_builder, crate::tokenize(source, BytePos(0))?);

let statement = context.parse_statement()?;
if context.token.token == Token::Eof {
Ok(statement)
} else {
Err(ParserError::unexpected(context.token.token, Token::Eof, context.token.span).into())
}
}
2 changes: 1 addition & 1 deletion compiler/span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl SourceMap {
}

/// Find the source file containing `pos`.
fn find_source_file(&self, pos: BytePos) -> Option<Rc<SourceFile>> {
pub fn find_source_file(&self, pos: BytePos) -> Option<Rc<SourceFile>> {
Some(self.inner.borrow().source_files[self.find_source_file_index(pos)?].clone())
}

Expand Down
50 changes: 50 additions & 0 deletions errors/src/errors/interpreter_halt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (C) 2019-2024 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/>.

use std::fmt;

use leo_span::Span;

/// Represents the interpreter halting, which should not be considered an
/// actual runtime error.
#[derive(Clone, Debug, Error)]
pub struct InterpreterHalt {
/// Optional Span where the halt occurred.
span: Option<Span>,

/// User visible message.
message: String,
}

impl InterpreterHalt {
pub fn new(message: String) -> Self {
InterpreterHalt { span: None, message }
}

pub fn new_spanned(message: String, span: Span) -> Self {
InterpreterHalt { span: Some(span), message }
}

pub fn span(&self) -> Option<Span> {
self.span
}
}

impl fmt::Display for InterpreterHalt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
7 changes: 7 additions & 0 deletions errors/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub use self::flattener::*;
pub mod loop_unroller;
pub use self::loop_unroller::*;

pub mod interpreter_halt;
pub use self::interpreter_halt::*;

/// Contains the Package error definitions.
pub mod package;
pub use self::package::*;
Expand Down Expand Up @@ -66,6 +69,8 @@ pub enum LeoError {
/// Represents a Compiler Error in a Leo Error.
#[error(transparent)]
CompilerError(#[from] CompilerError),
#[error(transparent)]
InterpreterHalt(#[from] InterpreterHalt),
/// Represents a Package Error in a Leo Error.
#[error(transparent)]
PackageError(#[from] PackageError),
Expand Down Expand Up @@ -110,6 +115,7 @@ impl LeoError {
UtilError(error) => error.error_code(),
LastErrorCode(_) => unreachable!(),
Anyhow(_) => "SnarkVM Error".to_string(), // todo: implement error codes for snarkvm errors.
InterpreterHalt(_) => "Interpreter Halt".to_string(),
}
}

Expand All @@ -129,6 +135,7 @@ impl LeoError {
UtilError(error) => error.exit_code(),
LastErrorCode(code) => *code,
Anyhow(_) => 11000, // todo: implement exit codes for snarkvm errors.
InterpreterHalt(_) => 1,
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions interpreter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "leo-interpreter"
version = "2.3.0"
authors = [ "The Leo Team <[email protected]>" ]
description = "Interpreter for the Leo programming language"
homepage = "https://leo-lang.org"
repository = "https://github.com/ProvableHQ/leo"
keywords = [
"aleo",
"cryptography",
"leo",
"programming-language",
"zero-knowledge"
]
categories = [ "compilers", "cryptography", "web-programming" ]
include = [ "Cargo.toml", "src", "README.md", "LICENSE.md" ]
license = "GPL-3.0"
edition = "2021"
rust-version = "1.69"

[dependencies.snarkvm-circuit]
version = "1.0.0"

[dependencies.snarkvm]
workspace = true

[dependencies.leo-ast]
workspace = true

[dependencies.leo-passes]
workspace = true

[dependencies.leo-errors]
workspace = true

[dependencies.leo-parser]
workspace = true

[dependencies.leo-span]
workspace = true

[dependencies.colored]
workspace = true

[dependencies.indexmap]
workspace = true
Loading

0 comments on commit 9e1108e

Please sign in to comment.