Created
June 24, 2014 21:08
-
-
Save erszcz/ec63931aa9bec985fd3f to your computer and use it in GitHub Desktop.
Rust: invalid memory reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(phase, globs)] | |
#[phase(syntax, link)] extern crate log; | |
fn tokenize(s: &str) -> ~[&str] { | |
let t = std::str::replace(s, "(", " ( "); | |
let u = std::str::replace(t, ")", " ) "); | |
u.words().collect() | |
} | |
#[deriving(Eq, Show, Clone)] | |
pub enum SExp { | |
Atom (~str), | |
List (~[SExp]) | |
} | |
fn parse(tokens: ~[&str]) -> SExp { | |
println!("tokens = {}", tokens); | |
//log!(log::INFO, "parse({})", tokens); | |
let (_, sexp) = do_parse(0, tokens); | |
sexp | |
} | |
fn do_parse(mut i: uint, tokens: &[&str]) -> (uint, SExp) { | |
log!(log::INFO, "do_parse({}, {})", i, tokens); | |
if tokens.is_empty() { fail!("unexpected EOF") } | |
let token = tokens[i]; | |
if token == ")" { fail!("unexpected )") } | |
if token != "(" { return ( 1, Atom (token.to_owned()) ) } | |
let mut sexps: ~[SExp] = ~[]; | |
i += 1; | |
while tokens[i] != ")" { | |
log!(log::INFO, "tokens[{}] = '{}'", i, tokens[i]); | |
let (n, sexp) = do_parse(i, tokens); | |
sexps.push(sexp); | |
i += n; | |
} | |
(i, List (sexps)) | |
} | |
#[test] | |
fn tokenize_test() { | |
assert_eq!(~["(", "set!", "a", | |
"(", "*", "(", "+", "1", "2", ")", "3", ")", ")"], | |
tokenize("(set! a (* (+ 1 2) 3))")); | |
assert_eq!(~["(", ")", "asd"], tokenize("()asd")); | |
assert_eq!(~["asd"], tokenize("asd")); | |
assert_eq!(~["asd", "qwe"], tokenize("asd qwe")); | |
assert_eq!(~["asd", ")", "(", "qwe"], tokenize("asd)(qwe")); | |
} | |
fn tokeparse(s: &str) -> SExp { | |
parse(tokenize(s)) | |
} | |
#[test] | |
fn test_parse_1() { | |
assert_eq!(Atom (~"asd"), tokeparse("asd")); | |
} | |
#[test] | |
fn test_parse_2() { | |
let tokens = tokenize(~"(asd qwe)"); | |
println!("test_parse_2: tokens = {}", tokens); | |
assert_eq!(List (~[Atom (~"asd"), Atom (~"qwe")]), | |
parse(tokens)); | |
} | |
#[test] | |
fn test_parse_3() { | |
assert_eq!(List (~[List (~[Atom (~"asd")]), Atom (~"qwe")]), | |
tokeparse("((asd) qwe)")); | |
} | |
#[test] | |
fn test_parse_4() { | |
assert_eq!(List (~[List (~[Atom (~"asd")]), List (~[Atom (~"qwe")])]), | |
tokeparse("((asd) (qwe))")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22:36:41 erszcz @ x2 : ~/work/learning/rust-sexp (master *%) | |
$ RUST_LOG=test-parser2=4 ./test-parser2 test_parse_2 | |
running 1 test | |
do_parse(0, [e, t-p, rse, 2]) | |
test test_parse_2 ... FAILED | |
failures: | |
---- test_parse_2 stdout ---- | |
test_parse_2: tokens = [(, asd, qwe, )] | |
tokens = [(, asd, qwe, )] | |
task 'test_parse_2' failed at 'assertion failed: `(left == right) && (right == left)` (left: `List([Atom(asd), Atom(qwe)])`, right: `Atom(e)`)', parser2.rs:64 | |
failures: | |
test_parse_2 | |
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured | |
task '<main>' failed at 'Some tests failed', /home/erszcz/work/rust/src/libtest/lib.rs:215 | |
22:36:41 erszcz @ x2 : ~/work/learning/rust-sexp (master *%) | |
$ RUST_LOG=test-parser2=4 ./test-parser2 test_parse_2 | |
running 1 test | |
do_parse(0, [(, asd, qwe, )]) | |
tokens[1] = 'asd' | |
do_parse(1, [(, asd, qwe, )]) | |
tokens[2] = 'qwe' | |
do_parse(2, [(, asd, qwe, )]) | |
test test_parse_2 ... ok | |
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment