Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to create a FORTRAN cell in Mathematica.

pr = "PROGRAM test
Integer::a, b
a=3
b=9
print*,  a, ' +', b, ' = ', a + b
print*,  a, ' *', b, ' = ', a * b
END Program test
";

GFortran[inp_] := (
Export["temp1.f90", inp, "Text"];
Column@ReadList["!gfortran temp1.f90 && a.exe", String])

The function works fine:

GFortran[pr]
DeleteFile[{"temp1.f90", "a.exe"}]

enter image description here

Now with a cell

(CellPrint[
 TextCell[
 pr, "Program",
 Evaluatable -> True, 
 CellEvaluationFunction -> (GFortran[#] &), 
 CellFrameLabels -> {{None, "Fortran"}, {None, None}}]]; 
SelectionMove[EvaluationNotebook[], Previous, CellContents])

enter image description here

File temp1.f90 is created, but not compiled, so there is no output. What could be wrong?

Edit:

I changed GFortran to

GFortran[inp_] := (
Export["temp1.f90", inp, "Text"];
Get["!gfortran temp1.f90"];
Column@ReadList["!a.exe", String])

Now the FORTRAN cell will have output, but only if temp1.f90 was compiled before cell execution

share|improve this question
    
What happens if you try something generic like ReadList["!pause", String], or a simple file copy, etc., to see if the command is being run? –  Mr.Wizard Jul 11 '12 at 1:35
    
For CellEvaluationFunction -> ReadList["!pause", String] output is {Press any key to continue . . .}(,TextForm) –  FDSg Jul 11 '12 at 1:45
    
By the way, I think you mean to have pr, "Program", in your TextCell expression. –  Mr.Wizard Jul 11 '12 at 2:19
1  
What happens if you use GFortran[inp_, _] := and CellEvaluationFunction -> GFortran ? –  Mr.Wizard Jul 11 '12 at 2:21
    
It worked! Changed to GFortran[inp_, _] now everything compiles. –  FDSg Jul 11 '12 at 2:32
show 1 more comment

1 Answer

up vote 7 down vote accepted

Since the CellEvaluationFunction is supposed to accept two arguments, the second being the format type, I recommend defining:

GFortran[inp_, _] := . . .

and then using it directly:

CellEvaluationFunction -> GFortran
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.