private void Body_() { new Validator("body") .OneParameter() .SymbolOrStringOnTop() .Validate(this.stack); var x = this.Pop(); var name = x switch { Value.String s => s.Value, Value.Symbol s => s.Value, _ => throw new NotSupportedException(), }; if (this.usrdefs.TryGetValue(name, out var usr)) { this.Push(usr); return; } var msg = $"undefined `{name}`"; throw new RuntimeException(msg); }
private void Import_() { var s = this.Pop(); var stream = s switch { Value.String x => File.OpenRead(x.Value), Value.Stream x => x.Value, _ => throw new NotSupportedException(), }; using (var reader = new StreamReader(stream)) { var source = reader.ReadToEnd(); var tokens = Parser.Tokenizer.Tokenize(source); var defs = Parser.Def.Many().Parse(tokens); Array.ForEach( defs.ToArray(), x => this.AddUsrdef(x.Id, x.Body)); } }
private void Name_() { new Validator("name") .OneParameter() .Validate(this.stack); var sym = this.Pop(); var name = sym switch { Value.Int x => "int", Value.Float x => "float", Value.Bool x => "bool", Value.Char x => "char", Value.String x => "string", Value.List x => "list", Value.Set x => "set", Value.Stream x => "stream", Value.Symbol x => x.Value, _ => throw new NotSupportedException(), }; this.Push(new Value.String(name)); }