Просмотр исходного кода

new In- and Out- Modules for educational purposes (can replace use of Commands.Context)

git-svn-id: https://svn.inf.ethz.ch/svn/lecturers/a2/trunk@7397 8c9fc860-2736-0410-a75d-ab315db34111
felixf 7 лет назад
Родитель
Сommit
c67e11e384
4 измененных файлов с 130 добавлено и 0 удалено
  1. 33 0
      source/In.Mod
  2. 37 0
      source/InOutExample.Mod
  3. 59 0
      source/Out.Mod
  4. 1 0
      source/Release.Tool

+ 33 - 0
source/In.Mod

@@ -0,0 +1,33 @@
+MODULE In; (** AUTHOR "FOF"; PURPOSE "Simple argument parser for educational purposes"; *)
+
+IMPORT Commands, Streams;
+
+PROCEDURE GetArg*(): Streams.Reader;
+BEGIN
+	RETURN Commands.GetContext().arg;
+END GetArg;
+
+PROCEDURE Char*(VAR ch: CHAR): BOOLEAN;
+BEGIN
+	RETURN GetArg().GetChar(ch);
+END Char;
+
+PROCEDURE Int*(VAR i: LONGINT; hex = TRUE: BOOLEAN): BOOLEAN;
+(* hex = TRUE means that hex numbers are allowed. A hex number must have the "H" postfix *)
+BEGIN
+	RETURN GetArg().GetInteger(i, hex);
+END Int;
+
+PROCEDURE Real*(VAR f: LONGREAL): BOOLEAN;
+BEGIN
+	RETURN GetArg().GetReal(f);
+END Real;
+
+PROCEDURE String*(VAR s: ARRAY OF CHAR): BOOLEAN;
+BEGIN
+	RETURN GetArg().GetString(s);
+END String;
+
+END In.
+
+

+ 37 - 0
source/InOutExample.Mod

@@ -0,0 +1,37 @@
+MODULE InOutExample; (** AUTHOR "FOF"; PURPOSE "simple demo of simple in and out"; *)
+
+IMPORT In, Out, Commands, Streams;
+
+(* thread safe when executed in isolated commands context, e.g. in PET
+	this does NOT imply that several commands in the same context would not produce unpredictable results.
+ *)
+PROCEDURE Hello*;
+VAR name: ARRAY 32 OF CHAR;
+BEGIN
+	IF In.String(name) THEN 
+		Out.String("Hello "); Out.String(name); Out.Ln;
+	ELSE
+		Out.String("Hello World!"); Out.Ln;
+	END;
+END Hello;
+
+PROCEDURE Number*;
+VAR r: LONGREAL; name: ARRAY 32 OF CHAR;
+BEGIN
+	IF In.String(name) & In.Real(r) THEN 
+		Out.String(name); Out.String(" = "); Out.Float(r); Out.Ln;
+		Out.String(name); Out.String(" = "); Out.Float(r,20); Out.Ln;
+		Out.String(name); Out.String(" = "); Out.Float(r,20,15,-1); Out.Ln;
+	END;
+END Number;
+
+
+END InOutExample.
+
+SystemTools.Free InOutExample In Out  ~
+
+InOutExample.Hello Oberon ~
+InOutExample.Hello ~
+InOutExample.Number pi 	3.14159265358979323846E0;
+~
+

+ 59 - 0
source/Out.Mod

@@ -0,0 +1,59 @@
+MODULE Out; (** AUTHOR "FOF"; PURPOSE "Simple console output, for educational purposes"; *)
+
+(* threadsafe as far as commands don't share the same context *)
+
+IMPORT Commands, Streams;
+
+PROCEDURE GetWriter*(): Streams.Writer;
+BEGIN
+	RETURN Commands.GetContext().out;
+END GetWriter;
+
+PROCEDURE String*(CONST s: ARRAY OF CHAR);
+BEGIN
+	Commands.GetContext().out.String(s);
+END String;
+
+PROCEDURE Char*(c: CHAR);
+BEGIN
+	Commands.GetContext().out.Char(c);
+END Char;
+
+PROCEDURE Ln*();
+BEGIN
+	Commands.GetContext().out.Ln();
+END Ln;
+
+PROCEDURE Set*(s: SET);
+BEGIN
+	Commands.GetContext().out.Set(s);
+END Set;
+
+PROCEDURE Int*(i: HUGEINT; n = 1: LONGINT);
+BEGIN
+	Commands.GetContext().out.Int(i,n);
+END Int;
+
+PROCEDURE Hex*(i: HUGEINT; n = -16: LONGINT);
+BEGIN
+	Commands.GetContext().out.Hex(i,n);
+END Hex;
+
+PROCEDURE Address*(a: ADDRESS);
+BEGIN
+	Commands.GetContext().out.Address(a);
+END Address;
+
+PROCEDURE Float*(x: LONGREAL; n = 4, f= 3, d=0: LONGINT);
+BEGIN
+	Commands.GetContext().out.FloatFix(x,n,f,d);
+END Float;
+
+
+
+END Out.
+
+SystemTools.Free Out ~
+Out.Hello 
+
+

+ 1 - 0
source/Release.Tool

@@ -432,6 +432,7 @@ PACKAGE System ARCHIVE "System.zip" SOURCE "SystemSrc.zip" DESCRIPTION "System"
 
 	# Services and device drivers
 	Plugins.Mod Streams.Mod Pipes.Mod Commands.Mod
+	In.Mod Out.Mod
 
 	I386, WIN, UNIX32, UNIXG32 { I386.Reals.Mod }
 	AMD64, UNIX64 { AMD64.Reals.Mod }